开发者

Objective C - Convert char[] to NSString in Hex format

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开发者_开发问答


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];


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜