www.webdeveloper.com
Results 1 to 5 of 5

Thread: casting problem char to int

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    casting problem char to int

    hey guys I have a problem with casting every char of a string into a integervalue to pass it into a database.

    I already know that there is a function like str.charCodeAt(i) to get an integer value, but for larger number above 180 it suddenly does not work.

    here is an example

    0xfd = 253 with charCodeAt(i) i receive 65533 the same holds for 0xb4 = 180 with charCodeAt(i) i get also the same wrong value.

    So where is my problem? Is there maybe another way of casting these values?

    Furthermore here is an example of the actual string i receive from the server..

    <Buffer 02 00 01 00 02 00 43 64 fd fd 00 01 00 b4 00 85 27 76 21 31 31 ee 8e 86 03 02 00 01 00 02 00 24 64 fd fd 00 01 01 b4 00 c3 27 76 21 31 31 ed 99 5c 03>

    And here is what comes out with charCodeAt():

    2
    0
    1
    0
    2
    0
    40
    100
    65533
    65533
    0
    1
    0
    65533
    0
    65533
    39
    118
    33
    49
    49
    65533
    65533
    94
    3

    This string is not the exact from the buffer above but you will see the bigger values dont match up at all..

    I thank you for your help and attention..

  2. #2
    Join Date
    Mar 2009
    Posts
    452
    what is the intention to do this ? and also post your code you are using currently, it will help us find the solution

  3. #3
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    I'd also be interested in knowing what this is intended to do -- big picture. Wouldn't hurt to show your existing source as well -- charCodeAt() works perfectly fine for me on that string as given:

    PHP Code:
    var "<Buffer 02 00 01 00 02 00 43 64 fd fd 00 01 00 b4 00 85 27 76 21 31 31 ee 8e 86 03 02 00 01 00 02 00 24 64 fd fd 00 01 01 b4 00 c3 27 76 21 31 31 ed 99 5c 03>";
    var 
    rv = [];
    for (var 
    i in s) {
      
    rv.push(s.charCodeAt(i));
    }
    console.log(rv.join(',')); 
    60,66,117,102,102,101,114,32,48,50,32,48,48,32,48,49,32,48,48,32,48,50,32,48,48,32,52,51,32,54,52,32 ,102,100,32,102,100,32,48,48,32,48,49,32,48,48,32,98,52,32,48,48,32,56,53,32,50,55,32,55,54,32,50,49 ,32,51,49,32,51,49,32,101,101,32,56,101,32,56,54,32,48,51,32,48,50,32,48,48,32,48,49,32,48,48,32,48, 50,32,48,48,32,50,52,32,54,52,32,102,100,32,102,100,32,48,48,32,48,49,32,48,49,32,98,52,32,48,48,32, 99,51,32,50,55,32,55,54,32,50,49,32,51,49,32,51,49,32,101,100,32,57,57,32,53,99,32,48,51,62

  4. #4
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    You may have to throw away the high order byte, as shown in this example:

    https://developer.mozilla.org/en-US/...older_browsers

    I have yet to find out why those high order bytes are attached, nonetheless, it's there, and you'll have to account for it.

    EDIT: This post gives a better explanation why those high order bytes exist. http://web.archive.org/web/200812190...eams-with.html
    Last edited by Jeff Mott; 10-09-2012 at 05:56 PM.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  5. #5
    Join Date
    Jan 2011
    Posts
    123
    I'm not being able to understand what are you trying to make with it, or (is it) make from it?

    Never minding that;
    -using my codezz, on your sample
    I get:
    Code:
    var Buffer = "02 00 01 00 02 00 43 64 fd fd 00 01 00 b4 00 85 27 76 21 31 31 ee 8e 86 03 02 00 01 00 02 00 24 64 fd fd 00 01 01 b4 00 c3 27 76 21 31 31 ed 99 5c 03"; // than from:
    Buffer ="0x" + Buffer.split(/ /).join(",0x");
    Buffer.fromChars(); 
    >>"Cd…'v!11Ž†$d'v!11™\"
    which looks something like this; or to say: a lot shorter; or at least better suited for storage purposes.
    In any case you are using some very unsafe chars there.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles