2

What I need to do is Convert a String to byte array with ASCII encoding… I’m using Swift and I can see the utf-8, utf-16 and unicodeScalars but no Ascii encoding found for String.

How can I do it? For example for a string like “ID00000004” the content of the bytes should be “49443030303030303034”

Can you help me? Thanks in advance

3
  • 3
    For all characters in the ASCII character set (U+0000 .. U+007F), the UTF-8 encoding and the ASCII encoding are identical.
    – Martin R
    Dec 17, 2014 at 17:45
  • @MartinR You may want to post your earlier comment as an answer, given that the answer it was posted on has been deleted. Dec 17, 2014 at 21:23
  • Hi, Yes, it's true but the result is not the correct one... I'm new with swift so maybe I'm doing the things wrong... I tried as follow: var ID_Centrale : [Byte] = [UInt8](myString.utf8) but the result is "73684848484848484852" instead of "49443030303030303034" I tried also this code: var ID_Centrale = [Byte]() for char in centrale.id_centrale.utf8{ ID_Centrale += [char] } Why?
    – Enricosoft
    Dec 18, 2014 at 8:28

1 Answer 1

-1

the following code is correct, the result was printed in console in decimal instead of hex

var result : [Byte] = UInt8

Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.