生活随笔
收集整理的這篇文章主要介紹了
iOS中NSString转换成HEX(十六进制)-NSData转换成int
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
NSString?*str?=?@"0xff055008";??unsigned?long?red?=?strtoul([str?UTF8String],0,16);??unsigned?long?red?=?strtoul([@"0x6587"?UTF8String],0,0);??NSLog(@"轉(zhuǎn)換完的數(shù)字為:%lx",red);???
[objc] view plaincopy
+?(NSString?*)stringFromHexString:(NSString?*)hexString?{????charchar?*myBuffer?=?(charchar?*)malloc((int)[hexString?length]?/?2?+?1);???bzero(myBuffer,?[hexString?length]?/?2?+?1);???for?(int?i?=?0;?i?<?[hexString?length]?-?1;?i?+=?2)?{???unsigned?int?anInt;???NSString?*?hexCharStr?=?[hexString?substringWithRange:NSMakeRange(i,?2)];???NSScanner?*?scanner?=?[[[NSScanner?alloc]?initWithString:hexCharStr]?autorelease];???[scanner?scanHexInt:&anInt];???myBuffer[i?/?2]?=?(char)anInt;???}???NSString?*unicodeString?=?[NSString?stringWithCString:myBuffer?encoding:4];???NSLog(@"------字符串=======%@",unicodeString);???return?unicodeString;?????????}?????????+?(NSString?*)hexStringFromString:(NSString?*)string{???NSData?*myD?=?[string?dataUsingEncoding:NSUTF8StringEncoding];???Byte?*bytes?=?(Byte?*)[myD?bytes];???NSString?*hexStr=@"";???for(int?i=0;i<[myD?length];i++)??????{???NSString?*newHexStr?=?[NSString?stringWithFormat:@"%x",bytes[i]&0xff];???if([newHexStr?length]==1)??????hexStr?=?[NSString?stringWithFormat:@"%@0%@",hexStr,newHexStr];??????else??????hexStr?=?[NSString?stringWithFormat:@"%@%@",hexStr,newHexStr];???}???return?hexStr;???}??? [objc] view plaincopy
int?i?=?1;???NSData?*data?=?[NSData?dataWithBytes:?&i?length:?sizeof(i)];???int?i;???[data?getBytes:?&i?length:?sizeof(i)];??
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/9155380.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔為你收集整理的iOS中NSString转换成HEX(十六进制)-NSData转换成int的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。