I need convert String to Unicode,like this: let str = "哈哈123abc" Because I send data is limited, so I need the most saving way,what should i do
-
2What String representation are you starting with? What is the String representation that you want to convert to? Take a look at the Strings and Characters documentation and see if it helps to improve your question.user887210– user8872102016-10-19 11:25:49 +00:00Commented Oct 19, 2016 at 11:25
-
I am used to BLE peripheral. The name, will be limited to 18 byte, including Chinese characters, Numbers and characters, Chinese characters 3 byte, because unicode is a 2 byte, so need to string converted to unicodesumberWang– sumberWang2016-10-19 11:31:06 +00:00Commented Oct 19, 2016 at 11:31
-
1What encoding does the Chinese characters have? Do you want to end up with UTF-8?user887210– user8872102016-10-19 11:42:13 +00:00Commented Oct 19, 2016 at 11:42
-
I need to convert any character to 2 bytesumberWang– sumberWang2016-10-19 11:51:36 +00:00Commented Oct 19, 2016 at 11:51
Add a comment
|
1 Answer
This way you can convert String to Unicode in swift3
var str : String = "哈哈123abc"
//String to Unicode
var dataenc = str.data(using: String.Encoding.nonLossyASCII)
var encodevalue = String(data: dataenc!, encoding: String.Encoding.utf8)
//Unicode to String
var datadec = encodevalue?.data(using: String.Encoding.utf8)
var decodevalue = String(data: datadec!, encoding: String.Encoding.nonLossyASCII)
3 Comments
sumberWang
oh,thanks,but,how convert Unicode to String in swift3?
Ishika
How do we do this for attributed string?
Vignesh
encode value gives two slash and it's not working, please check your answer again