How to convert NSString to bytes

2019-01-25 06:23发布

NSString *test = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

How to convert this string to bytes?

标签: nsstring
3条回答
Root(大扎)
2楼-- · 2019-01-25 06:28
NSData *bytes = [test dataUsingEncoding:NSUTF8StringEncoding];
查看更多
该账号已被封号
3楼-- · 2019-01-25 06:30
extension String {
    var byteArray : [Byte] {
        return Array(utf8)
    }
}

update: Xcode 7.2.1 • Swift 2.1.1

extension String {
    var byteArray : [UInt8] {
        return Array(utf8)
    }
}

testing:

"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]
查看更多
相关推荐>>
4楼-- · 2019-01-25 06:46

Do you want something like this:

NSString *test=@"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSUInteger bytes = [test lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%i bytes", bytes);
查看更多
登录 后发表回答