Reputation: 53
I have a problem I need to convert char [] to String, hexadecimal format
Example
Nsstring * result;
char[4]={0x01,0x02,0x03,0x04};
/// convert -- char[] -> Nsstring in Hex format
Nslog(@"%@",result);
expected output: "01020304"
Thanks
Upvotes: 5
Views: 5900
Reputation: 586
try this:
NSMutableString * result = [[NSMutableString alloc] init]; char cstring[4]={0x01,0x02,0x03,0x04}; /// convert -- char[] -> Nsstring in Hex format int i; for (i=0; i<4; i++) { [result appendString:[NSString stringWithFormat:@"%02x",cstring[i]]]; } NSLog(@"%@",result); [result release];
Upvotes: 10
Reputation: 70733
The formatter "%02x" will display an individual character as 2 digits of zero-padded hex. Simply loop though your array and build the string with them.
Upvotes: 4