2

I receive Array with bytes:

var requestPtr = [3, 64, 0, 73, 0, 110, 0, 116, 0, 101, 0, 114, 0, 110, 0, 97, 0, 108, 0, 32, 0, 70, 0, 108, 0, 97, 0, 115, 0, 104, 0, 32, 0, 32, 0, 47, 0, 48, 0, 120, 0, 48, 0, 56, 0, 48, 0, 48, 0, 48, 0, 48, 0, 48, 0, 48, 0, 47, 0, 48, 0, 52, 0, 42, 0, 48, 0, 49, 0, 54, 0, 75, 0, 103, 0, 44, 0, 48, 0, 49, 0, 42, 0, 48, 0, 54, 0, 52, 0, 75, 0, 103, 0, 44, 0, 48, 0, 55, 0, 42, 0, 49, 0, 50, 0, 56, 0, 75, 0, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Then remove first byte and convert array to string with this code:

requestPtr.remove(at: 0)

let nsdata = NSData(bytes: requestPtr, length: requestPtr.count)
var str = String(data: nsdata as Data, encoding: String.Encoding.utf8)!

If i do print in console it returns me string: enter image description here But i get weird encoding, when try to copy message.

enter image description here

And functions like str.hasPrefix("@Internal Flash") not working.

And var tmp1 = description.components(separatedBy: ["/"]) returns:

["\u{03}@\0I\0n\0t\0e\0r\0n\0a\0l\0 \0F\0l\0a\0s\0h\0 \0 \0", "\00\0x\00\08\00\00\00\00\00\00\0", "\00\04\0*\00\01\06\0K\0g\0,\00\01\0*\00\06\04\0K\0g\0,\00\07\0*\01\02\08\0K\0g\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"]

3 Answers 3

1

There is a need to define the byte array of type [UInt8]. Below mentioned will work and return True

var requestPtr:[UInt8] = [3, 73, 110, 116, 101, 114, 110, 97, 108, 32, 70, 108, 97, 115, 104] requestPtr.remove(at: 0) print(requestPtr.count) print(requestPtr) let nsdata = NSData(bytes: requestPtr as [UInt8], length: requestPtr.count) var str = String(data: nsdata as Data, encoding: String.Encoding.utf8)! str.hasPrefix("Internal Flash")

Sign up to request clarification or add additional context in comments.

1 Comment

@Arti in your original code array is define with implicit type conversion due to which it's being converted differently when byte array is being converted to NSData. In my answer code byte array is explicitly defined as array of UInt8.
1

It seems the data is encoded in UTF-16LE (little endian UTF-16), not in UTF-8.

(Code updated to clarify, with what data I have got the result shown.)

var requestPtr = [3, 64, 0, 73, 0, 110, 0, 116, 0, 101, 0, 114, 0, 110, 0, 97, 0, 108, 0, 32, 0, 70, 0, 108, 0, 97, 0, 115, 0, 104, 0, 32, 0, 32, 0, 47, 0, 48, 0, 120, 0, 48, 0, 56, 0, 48, 0, 48, 0, 48, 0, 48, 0, 48, 0, 48, 0, 47, 0, 48, 0, 52, 0, 42, 0, 48, 0, 49, 0, 54, 0, 75, 0, 103, 0, 44, 0, 48, 0, 49, 0, 42, 0, 48, 0, 54, 0, 52, 0, 75, 0, 103, 0, 44, 0, 48, 0, 55, 0, 42, 0, 49, 0, 50, 0, 56, 0, 75, 0, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

requestPtr.remove(at: 0)

let data = Data(bytes: requestPtr.map{UInt8($0)})
var str = String(data: data, encoding: String.Encoding.utf16LittleEndian)!.trimmingCharacters(in: CharacterSet(charactersIn: "\0"))

debugPrint(str) //->"@Internal Flash  /0x08000000/04*016Kg,01*064Kg,07*128Kg"

(The actual data is said to be [UInt8], but .map{UInt8($0)} is making it.)

9 Comments

how do you detect encoding? this doesn't work 䀃䤀渀琀攀爀渀愀氀 䘀氀愀猀栀  ⼀ 砀 㠀      ⼀ 㐀⨀ ㄀㘀䬀最Ⰰ ㄀⨀ 㘀㐀䬀最Ⰰ 㜀⨀㄀㈀㠀䬀最
UTF-16LE is one possible candidate to interpret your byte sequence. And please explain how you get that this doesn't work string.
@Arti, I have found it. You have tried my code without your requestPtr.remove(at: 0). Please try my code after requestPtr.remove(at: 0).
@Arti, and one more thing you should know, USB devices use UTF16LE encoding.
@Arti, then you are mistaking something. I'm very sure my code works when put after var requestPtr = [...] and requestPtr.remove(at: 0). The output shown in my answer is taken from the actual output in the Playground.
|
0

You recevie array with byte, but only every second byte is correct

try this:

let test = String(requestPtr.enumerated().filter{ index, element in
    return index % 2 == 1
}.map { index, element in
    return Character(UnicodeScalar(element)! )
})

They are probably better solutions, but nothing comes to mind

3 Comments

thank you. Your solution works. Could you explain it please
i can try, but i need your function to recived bytes
i receive bytes from usb device: var request = IOUSBDevRequest(bmRequestType: 128, bRequest: 6, wValue: 0x300 | UInt16(index), wIndex: 0, wLength: 255, pData: &requestPtr, wLenDone: 255) kr = deviceInterface.DeviceRequest(deviceInterfacePtrPtr, &request)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.