0

Im trying to get this remote binary file to read the bytes, which (of course) are supossed to come in the range 0..255. Since the response is given as a string, I need to use charCodeAt to get the numeric values for every character. I have come across the problem that charCodeAt returns the value in UTF8 (if im not mistaken), so for example the ASCII value 139 gets converted to 8249. This messes up my whole application cause I need to get those value as they are sent from the server.

The immediate solution is to create a big switch that, for every given UTF8 code will return the corresponding ASCII. But i was wondering if there is a more elegant and simpler solution. Thanks in advance.

Kilian Perdomo Curbelo
  • 1,281
  • 1
  • 15
  • 31

2 Answers2

1

I would recommend encoding the binary data is some character-encoding independent format like base64

tobyodavies
  • 27,347
  • 5
  • 42
  • 57
  • That would be done by manipulating the data from the server side, wouldnt it? The problem with that approach is that i don't control the source of the data, since its a remote, external server. Well, actually im using a proxy script to bypass the same origin policy, so i guess i could do that there. But im trying to avoid that cause in the future i would like to take advantage of the CORS mechanism. And in that situation, i wouldnt be able to touch the data before it gets to the browser. – Kilian Perdomo Curbelo Feb 01 '12 at 09:17
1

The following code has been extracted from an answer to this StackOverflow question and should help you work around your issue.

function stringToBytesFaster ( str ) { 
    var ch, st, re = [], j=0;
    for (var i = 0; i < str.length; i++ ) { 
        ch = str.charCodeAt(i);
        if(ch < 127)
        {
            re[j++] = ch & 0xFF;
        }
        else
        {
            st = [];    // clear stack
            do {
                st.push( ch & 0xFF );  // push byte to stack
                ch = ch >> 8;          // shift value down by 1 byte
            }
            while ( ch );
            // add stack contents to result
            // done because chars have "wrong" endianness
            st = st.reverse();
            for(var k=0;k<st.length; ++k)
                re[j++] = st[k];
        }
    }   
    // return an array of bytes
    return re; 
}

var str = "\x8b\x00\x01\x41A\u1242B\u4123C";

alert(stringToBytesFaster(str)); // 139,0,1,65,65,18,66,66,65,35,67
Community
  • 1
  • 1
nulltoken
  • 64,429
  • 20
  • 138
  • 130