swift 如何将字符串转换为字节数组(EUC-KR)?

x6492ojm  于 2022-12-10  发布在  Swift
关注(0)|答案(1)|浏览(142)

Hello There i am using swift 5.0 and developing BLE App.
As we have android app there are using default function as below

byte nByte[] = Name.getBytes( charsetName: "EUC-KR")

Output of android

Value[0] = 32 Value[1] = 30 Value[2] = 32 Value[3] = 32 Value[4] = 31 Value[5] = 31 Value[6] = 32 Value[7] = 37 Value[8] = 2d Value[9] = c3 Value[10] = e6 Value[11] = ba Value[12] = cf Value[13] = 38 Value[14] = 30 Value[15] = c0 Value[16] = da Value[17] = 39 Value[18] = 30 Value[19] = 31 Value[20] = 35 Value[21] = 2d Value[22] = 58 Value[23] = 2d Value[24] = 30 Value[25] = 32 Value[26] = 2d Value[27] = 31 Value[28] = 32 Value[29] = 31 Value[30] = 32 Value[31] = 31 Value[32] = 32 Value[33] = 31 Value[34] = 2e Value[35] = 54 Value[36] = 58 Value[37] = 54
We used in iOS different type of string convert as below

  • Code 1*
let  rawEncoding =  CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.EUC_KR.rawValue))
let encoding =  String.Encoding(rawValue: rawEncoding)
let  strEUCData = "20221127-충북80자9015-X-02-1212121.TXT".data(using:  encoding) ?? Data()

Output of iOS

bytes : 38 elements

  • 0 : 50
  • 1 : 48
  • 2 : 50
  • 3 : 50
  • 4 : 49
  • 5 : 49
  • 6 : 50
  • 8 : 45
  • 9 : 195
  • 10 : 230
  • 11 : 186
  • 12 : 207
  • 13 : 56
  • 14 : 48
  • 15 : 192
  • 16 : 218
  • 17 : 57
  • 18 : 48
  • 19 : 49
  • 20 : 53
  • 21 : 45
  • 22 : 88
  • 23 : 45
  • 24 : 48
  • 25 : 50
  • 26 : 45
  • 27 : 49
  • 28 : 50
  • 29 : 49
  • 30 : 50
  • 31 : 49
  • 32 : 50
  • 33 : 49
  • 34 : 46
  • 35 : 84
  • 36 : 88
  • 37 : 84
  • Code 2*
let  strEUCData1 = "20221127-충북80자9015-X-02-1212121.TXT".data(using:  String.Encoding(rawValue: 0x80000940)) ?? Data()

All above functions given wrong byte array.
Any help will be appreciated. Thank you.

c3frrgcw

c3frrgcw1#

它们是相同的数据。Android的输出是十六进制(以16为基数)数字,iOS的输出是十进制(以10为基数)数字:

Android (Hex)   iOS (Decimal)
32              50
30              48
32              50
32              50
31              49
31              49
32              50
37              55
2d              45
c3              195
e6              230
ba              186
cf              207
38              56
30              48
c0              192
da              218
39              57
30              48
31              49
35              53
2d              45
58              88
2d              45
30              48
32              50
2d              45
31              49
32              50
31              49
32              50
31              49
32              50
31              49
2e              46
54              84
58              88
54              84

相关问题