字符串转16进制 16进制转字符串
测试网站 http://www.bejson.com/convert/ox2str/
//e8afb7e58187e58d95313532373836e8bf98e8819ae9a490666a68676a736a6a646a64
//请假单152786还聚餐fjhgjsjjdjd
//普通字符串转换为十六进制的。
- (NSString *)hexStringFromString:(NSString *)string{
NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
Byte *bytes = (Byte *)[myD bytes];
//下面是Byte 转换为16进制。
NSString *hexStr=@"";
for(int i=0;i<[myD length];i++)
{
NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
if([newHexStr length]==1)
hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
else
hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
}
return hexStr;
}
// 十六进制转换为普通字符串的。
- (NSString *)stringFromHexString:(NSString *)hexString {
char *myBuffer = (char *)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];
[scanner scanHexInt:&anInt];
myBuffer[i / 2] = (char)anInt;
}
NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
NSLog(@"------字符串=======%@",unicodeString);
return unicodeString;
}
16进制转NSData NSData转16进制
// 16进制转NSData
- (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;
}
// NSData转16进制 第一种
- (NSString *)convertDataToHexStr:(NSData *)data
{
if (!data || [data length] == 0) {
return @"";
}
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
[data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
unsigned char *dataBytes = (unsigned char*)bytes;
for (NSInteger i = 0; i < byteRange.length; i++) {
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
if ([hexStr length] == 2) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr];
}
}
}];
return string;
}
// NSData转16进制 第二种
-(NSString *)hexStringFormData:(NSData *)data
{
return [[[[NSString stringWithFormat:@"%@",data]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
}
问题
- 在bejson网站测试的 换行的字符串 转成的 16进制进制
16进制到文本字符串的转换
换行
在线实时转换
用网站转成
3136e8bf9be588b6e588b0e69687e69cace5ad97e7aca6e4b8b2e79a84e8bdace68da2ae68da2e8a18cae59ca8e7babfe5ae9ee697b6e8bdace68da2
用代码 stringFromHexString
不能把这个16进制再转回来
- 在代码设置 \n换行 是可以识别 并转换的
16进制到文本字符串的转换\n换行\n在线实时转换
用代码hexStringFromString
转成
3136e8bf9be588b6e588b0e69687e69cace5ad97e7aca6e4b8b2e79a84e8bdace68da20ae68da2e8a18c0ae59ca8e7babfe5ae9ee697b6e8bdace68da2
用代码stringFromHexString
回转
16进制到文本字符串的转换
换行
在线实时转换
- 分析对照下
换行符不一样
3136e8bf9be588b6e588b0e69687e69cace5ad97e7aca6e4b8b2e79a84e8bdace68da2
a
e68da2e8a18c
a
e59ca8e7babfe5ae9ee697b6e8bdace68da2
3136e8bf9be588b6e588b0e69687e69cace5ad97e7aca6e4b8b2e79a84e8bdace68da2
0a
e68da2e8a18c
0a
e59ca8e7babfe5ae9ee697b6e8bdace68da2
中文字符串转NSData GBK 和 UTF-8
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData *valData = [hextext dataUsingEncoding:enc];
待补充
网友评论