0

Possible Duplicate:
NSString (hex) to bytes

I'm currently stuck trying to convert the contents of a NSString to a byte.

My program receives a string, for example CC to a byte 0xCC

I understand that you can convert an NSString to its hex representation, but this isn't what I need, as that uses the string encoding -- my CC would turn out as 0x63.

From what I understand, I need to cast the NSString (or char) to a raw byte, so that I can then later on convert it to a decimal but i'm getting really confused on how to do it.

Community
  • 1
  • 1
liamnichols
  • 12,419
  • 2
  • 43
  • 62
  • So you receive a string which contains just hex characters (i.e. 0-9 A-F) and want to convert it to byte(s)? So `"CC"` => `0xCC` and `"ABCD"` => `0xAB 0xCD`? – mattjgalloway Nov 28 '11 at 21:55

1 Answers1

0

Not 100% sure if this is what you mean but

NSData *bytes = [@"hello" dataUsingEncoding:NSUTF8StringEncoding];

would create a byte array.

Macmade
  • 52,708
  • 13
  • 106
  • 123
Michael Smith
  • 498
  • 2
  • 5
  • 15