Lefora Free Forum
login join
Loading
434 views

unicode conversions

Page 1
1–3
regular - member
54 posts

I have gotten myself in deep again! I have an NSData object that contains image data and need to convert bytes to Unicode values. I have almost no experience using raw data. What I need to do is get the Unicode value of the character at byte position 0 and add add  0xff.

The Data(partial):

47494638 39614500 1d008400 00ffffff f0f0f0e0 e0e0d0d0 d0c0c0c0 b0b0b0a0 a0a09090 90808080 70707060 60605050 50404040

The answer to the problem is: 71. But I cannot figure out why. I am adapting a javascript code to work in my project.

The javascript is:         data_array.push(data[i].charCodeAt(0) & 0xff);

which translates to:
  add to data_array:
    the unicode value of the byte i and add 0xff to that -> for byte 0 the answer is 71.

Any ideas?

Todd





?
288 posts


Todd

You've printed the data in Hex.  So the first byte in 47 (hex) = 4*16 + 7 = 71.  The charCodeAt(0) & 0xff is clipping the Unicode (in which a character can be more that 1 byte).  So the Javascript is pushing integers (0..255) onto an array.

Can I ask about the relationship between the JavaScript and the Cocoa? Are you trying to write the NSData* binary data out into a string that can be read by JavaScript?

__________________
regular - member
54 posts

Okay. So 0-255 will fit into a single byte? Anything higher is more than one byte, right?

So is there an easier cocoa way of accomplishing this? 0-255 makes sense as in the end I am getting a pixels color. charCodeAt(0) & 0xff is getting one byte? That is easily accomplished using:
    NSData *data2 = [imageData subdataWithRange:range];

So is there an easier way to go from 47(hex) to 71(unicode)? I see above I could probably use your formula, but seems like there should be a cool cocoa way of doing it.

Todd
   

Page 1
1–3

Locked Topic


You must be a member to post in this forum

Join Now!