Swift 2如何从ASCII编码为十六进制 [英] Swift 2 How to encode from ASCII to Hexadecimal

查看:276
本文介绍了Swift 2如何从ASCII编码为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的ASCII字符串,我想将其转换为十六进制(base16)。我使用xCode 7(所以我在IOS9上),但我找不到任何解决方案。



我试图改变我的字符串格式:

  StringToConvert(String(format:%02hhx,$ 0))
pre>

感谢您的帮助:)

解决方案

abcdefghijklmnopqrstuvwxyz0123456789.utf8.map {$ 0} .reduce(){
$ 0 +字符串($ 1,基数:16,大写:false)
}

根据您的需要修改它

 ABCD.utf8.map {$ 0} .reduce(){
$ 0 +0x+ String($ 1,radix:16,uppercase:false)+
}
// 0x41 0x42 0x43 0x44


I have a simple ASCII string and i want to convert it to hexadecimal (base16). I'm using xCode 7 (so i'm on IOS9) but i can't find any solution anywhere.

I tried to change the format of my string:

StringToConvert(String(format: "%02hhx", $0))

Thanks for your help :)

解决方案

"abcdefghijklmnopqrstuvwxyz0123456789".utf8.map{ $0 }.reduce("") {
    $0 + String($1, radix: 16, uppercase: false)
}

modify it by your needs

"ABCD".utf8.map{ $0 }.reduce("") {
    $0 + "0x" + String($1, radix: 16, uppercase: false) + " "
}
// 0x41 0x42 0x43 0x44

这篇关于Swift 2如何从ASCII编码为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆