iOS 16進文字列、data変換
1333 ワード
dataを16進文字列に変換
通常文字列から16進文字列への変換
16進文字列転送data
-(NSString *)HexStringWithData:(NSData *)data{
Byte *bytes = (Byte *)[data bytes];
NSString *hexStr=@"";
for(int i=0;i
通常文字列から16進文字列への変換
- (NSString *)hexStringFromString:(NSString *)string{
NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
Byte *bytes = (Byte *)[myD bytes];
// Byte 16 。
NSString *hexStr=@"";
for(int i=0;i
16進文字列転送data
- (NSData *)convertHexStrToData:(NSString *)str
{
if (!str || [str length] == 0) {
return nil;
}
NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
NSRange range;
if ([str length] % 2 == 0) {
range = NSMakeRange(0, 2);
} else {
range = NSMakeRange(0, 1);
}
for (NSInteger i = range.location; i < [str length]; i += 2) {
unsigned int anInt;
NSString *hexCharStr = [str substringWithRange:range];
NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
[scanner scanHexInt:&anInt];
NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
[hexData appendData:entity];
range.location += range.length;
range.length = 2;
}
return hexData;
}