NSString *test = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
如何将此字符串转换为字节?
答案 0 :(得分:24)
NSData *bytes = [test dataUsingEncoding:NSUTF8StringEncoding];
答案 1 :(得分:3)
你想要这样的东西:
NSString *test=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSUInteger bytes = [test lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%i bytes", bytes);
答案 2 :(得分:1)
extension String {
var byteArray : [Byte] {
return Array(utf8)
}
}
更新: Xcode 7.2.1•Swift 2.1.1
extension String {
var byteArray : [UInt8] {
return Array(utf8)
}
}
测试:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".byteArray // [65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90]