1

Possible Duplicate:
Byte array in objective-c

Am converting some Java code to Objective-C and have run into an issue that I can't get my head around:

public static final byte[] DATA_GENERIC = new byte[] { (byte)0xA0, 0x00, 0x00, 0x00, 0x03,
            0x10, 0x10 };

Does anyone know to convert the above into Objective-C

Community
  • 1
  • 1
wibosco
  • 967
  • 1
  • 11
  • 32
  • This thread contains the information you need: http://stackoverflow.com/questions/876598/byte-array-in-objective-c – Perception Jul 13 '11 at 14:15
  • @Perception thanks for quick reply. I seen that post but couldn't take any further. I can create the char array but then don't know how to interact it using Objective-c – wibosco Jul 13 '11 at 14:29

1 Answers1

4

Here is an example of getting your data into a NSData object.

const unsigned char bytes[] = { 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10 };
NSData *data = [NSData dataWithBytes:bytes length:7];
NSLog(@"%@", data);

Output:

<a0000000 031010>

One major difference from java is you will need to keep track of the number of bytes yourself when working with a raw char array. Once you create the NSData you can access the length.

Joe
  • 56,979
  • 9
  • 128
  • 135
  • thanks very much worked a treat so is a Java type "Byte" equivalent to NSData – wibosco Jul 13 '11 at 14:49
  • NSData is closer to a Byte[]. Byte in Java is a class where in C/Objective-C it would be represented as an unsigned char. They are still quite different. – Joe Jul 13 '11 at 14:59