本文介绍了如何将文本字符串转换为十六进制字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以说我有一个文本字符串:
向我的小朋友问好
函数应该返回十六进制值:
5361792048656c6c6f20746f204d79204c6974746c6520467269656e64
解决方案
无法用swift编写,下面的代码可能是你正在寻找的:
NSString * str = @向我的小朋友问好;
NSString * hexString = [NSString stringWithFormat:@%@,
[NSDataDataWithBytes:[str cStringUsingEncoding:NSUTF8StringEncoding]
length:strlen([str cStringUsingEncoding:NSUTF8StringEncoding] )]];
for(NSArray arrayWithObjects:@<,@> @ ];
NSLog(@hexStr:%@,hexString);
上面的代码给出了确切的字符串:
5361792048656c6c6f20746f204d79204c6974746c6520467269656e64
希望它有助于:)
Lets say I have a text string :"Say Hello to My Little Friend"
A function should return hex value as:
5361792048656c6c6f20746f204d79204c6974746c6520467269656e64
解决方案
Couldn't able to write in swift, but in Objective-C below code is may be what you are looking for:
NSString * str = @"Say Hello to My Little Friend";
NSString * hexString = [NSString stringWithFormat:@"%@",
[NSData dataWithBytes:[str cStringUsingEncoding:NSUTF8StringEncoding]
length:strlen([str cStringUsingEncoding:NSUTF8StringEncoding])]];
for(NSString * toRemove in [NSArray arrayWithObjects:@"<", @">", @" ", nil])
hexString = [hexString stringByReplacingOccurrencesOfString:toRemove withString:@""];
NSLog(@"hexStr:%@", hexString);
Above code gives exact string as you given:
5361792048656c6c6f20746f204d79204c6974746c6520467269656e64
Hope it will help:)
这篇关于如何将文本字符串转换为十六进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!