可以說我有一個文本字符串: 「問好我的小朋友」如何將文本字符串轉換爲十六進制字符串?
函數應該返回十六進制值:
5361792048656c6c6f20746f204d79204c6974746c6520467269656e64
可以說我有一個文本字符串: 「問好我的小朋友」如何將文本字符串轉換爲十六進制字符串?
函數應該返回十六進制值:
5361792048656c6c6f20746f204d79204c6974746c6520467269656e64
莫非不能以迅速寫,但在下面的代碼的Objective-C是可能是你在找什麼:
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);
上面的代碼給你給出精確的字符串:
5361792048656c6c6f20746f204d79204c6974746c6520467269656e64
希望這將有助於:)
你可以做到這一點像,
NSString *str = @"Say Hello to My Little Friend";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"data %@",data);
Optput:data <53617920 48656c6c 6f20746f 204d7920 4c697474 6c652046 7269656e 64>
斯威夫特:
let string = "Say Hello to My Little Friend"
let data = string.dataUsingEncoding(NSUTF8StringEncoding)
斯威夫特3:
let string = "Say Hello to My Little Friend"
let data = string.data(using: String.Encoding.utf8)
斯威夫特4,斯威夫特3
let hex = "ABC".unicodeScalars.filter { $0.isASCII }.map { String(format: "%X", $0.value) }.joined() print(hex) // correctly yields 414243
是正確的什麼錯誤ü面對.. –