sumberWang
sumberWang

Reputation: 65

How can I convert a string to Unicode in swift?

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

Upvotes: 1

Views: 13568

Answers (1)

Chirag Desai
Chirag Desai

Reputation: 837

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)

Upvotes: 18

Related Questions