I’m trying to produce a string from an array of bytes in objective c. Within my class, i define the following constant char
static const char HELLO_WORLD[] = {0x68,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,0x6c,0x64,0x0d,0x0a};
I then have a method in MyClass that looks like this:
+ (NSString *)stringFromBytes:(const void*)bytes
{
return [[NSString alloc] initWithBytes:bytes length:(sizeof(bytes)/sizeof(bytes[0])) encoding:NSUTF8StringEncoding];
}
if I call the method by doing this
[MyClass stringFromBytes:HELLO_WORLD];
I get a string that looks like “hell” – im not sure if i’m doing the length part of this correctly, and im assuming thats the issue. Any guidance on how to get that working?
thanks!
Do this:
Use like this: