Reputation: 1287
I'm trying to convert a NSString
into int HEX array.
For instance if I have @"HELLO"
and I would like to get 0x48 0x45 0x4c 0x4c 0x4f
I have tried for each char of the string:
unsigned int hexInt;
sscanf([stringParam UTF8String], "%u", &hexInt);
and
unsigned int hexInt;
[[NSScanner scannerWithString:stringParam]scanHexInt:&hexInt];
None of them seem to work.
What is the best way to get: unsigned int res[]
from an ASCII NSString
?
Upvotes: 1
Views: 1010
Reputation: 6505
So, you want to store the characters of a string in an array of integers, right?
If you're OK with using unsigned short
(16 bits) instead of unsigned int
(32 bits), that's really easy.
NSUInteger length = [stringParam length];
unichar *characters = malloc(sizeof(unichar) * length);
[stringParam getCharacters:characters range:NSMakeRange(0, length)];
// Do whatever you want with the characters, then, once you're done...
free(characters);
Upvotes: 1
Reputation: 47759
Not really an answer, but I suspect that the original question is confused on the distinction between "hex" and "binary". All data internal to modern computers is manipulated in "bits" -- ones and zeros. An "integer" is generally 32 or 64 ones/zeros taken together. A "character" is 8, 16, or 32 ones/zeros, depending on the specific character set and context.
To allow humans to examine these electronic ones and zeros, they must be presented in some human-readable form. Simplest is "binary" -- eg, 00001010 binary has a numerical value of 10 (decimal). Since binary is not very compact, "octal" was invented. In octal the 00001010 binary value would be 012.
But most computer "words" are some multiple of 8 bits, and octal does not evenly represent multiples of 8 bits, so "hex" ("hexadecimal") was invented. In hex, 00001010 is represented as 0A.
What this means is that the only time you "convert" something to hex is to display it. But fairly frequently newbies get confused on this point and think that converting, say, a character to a numerical internal representation is "converting it to hex".
In fact, characters already have a numerical internal representation -- it's simply a matter of extracting the individual character and (optionally) "casting" it to an integer type (and note that "casting" does not change the value, just the type of variable it can be stored in).
In his last sentence, the OP indicated he wanted an array of int
values. The individual NSString characters already are integers, only of the unichar
type. "Converting" to integer is merely a matter of casting to the desired target type.
Upvotes: 2
Reputation: 5499
try with this function:
- (NSArray *)stringToHex:(NSString *)str
{
NSUInteger len = [str length];
unichar *chars = malloc(len * sizeof(unichar));
[str getCharacters:chars];
NSMutableArray *hexArray = [NSMutableArray array];
for(NSUInteger i = 0; i < len; i++ )
{
NSString *hexString = [NSString stringWithFormat:@"%02x", chars[i]];
[hexArray addObject:hexString];
}
free(chars);
return hexArray;
}
Upvotes: -1