0

I am trying to convert UUIDv4 into byte array for CloudSQL backend storage. I have removed the dashes, converted each char into its 4 bit hex binary string and have stitched them all back together into a 16 byte array.

But I am now trying to send this to a Node.js server and Alamofire 4 needs it as a string to send as a parameter. But I cannot convert "010001010" binary strings into characters. I can create Data or NSData from the byte array but cannot make the final conversion to String. Get the following error with the below code:

fatal error: unexpectedly found nil while unwrapping an Optional value

    let byteArray:[UInt8] = [0xa2, 0x00]

    let dataByteArray = Data(byteArray)

    let data = NSData(data: dataByteArray)

   Error here -->  let resultNSString = NSString(data: data as Data, encoding: String.Encoding.utf8.rawValue)!

    let resultString = resultNSString as String


    //let uuidString = String(data: data, encoding: .utf8)
    print(resultString)


    return resultString
}

Code example

T Rogers
  • 101
  • 6
  • It works OK if I directly Init Data with a String for example below, but when I try to create a Byte Array [Uint8] the Data Init does not seem to happen correctly : let dataByteArray:Data = "hello".data(using: String.Encoding.utf8)! – T Rogers Mar 13 '17 at 13:32
  • The string conversion fails because the data is not a valid UTF-8 sequence. Perhaps you want to sent a hex-encoded string? Compare http://stackoverflow.com/a/40089462/1187415. – Martin R Mar 13 '17 at 13:39
  • How can they not be valid UTF-8 sequences if they are all 8bit binary? – T Rogers Mar 13 '17 at 13:41
  • 1
    For example, `A2` is the leading byte of a 2-byte UTF-8 sequence. `A2 00` is invalid UTF-8. – What string would you expect as result from your byte array `[0xa2, 0x00]`? – Martin R Mar 13 '17 at 13:42
  • Apologies for my ignorance. Have been stuck on this for a while. I originally was trying to send it as Hex, but Swift doesn't like an Hex Integer that large. So have been trying to send it as a String but I cant do that now either I guess. 32 bytes per UUID it is I guess. – T Rogers Mar 13 '17 at 13:48
  • What is the UUIDv4, and what is the intended result string? – Martin R Mar 13 '17 at 13:52
  • A 128 bit globally unique identifier generated by UUID() in swift. For use as primary keys in the backend – T Rogers Mar 13 '17 at 13:54
  • It is still unclear to me why you are doing all those conversions. Just send `uuid.uuidString` to the server (remove the dashes from the string if you like). – Martin R Mar 13 '17 at 13:58
  • because it then takes up double the storage space on the backend 32 bytes as opposed to 16. When hopefully user numbers increase indexing / uniqueness queries will slow down exponentially no? – T Rogers Mar 13 '17 at 14:01
  • The UUID is a blob of 16 binary bytes and does not represent a string. Your issue is essentially the same as this one: http://stackoverflow.com/questions/41650817/cant-convert-encrypted-data-to-string. – Even *if* the data happens to be valid UTF-8: 0x0A would be translated to a newline, and 0x0D to a carriage return. Both are probably *not* transmitted correctly to the server. – Martin R Mar 13 '17 at 14:03
  • But as you mentioned I can only send a String to the server which then allocated 2 bytes per character as soon as I call uuid.uuidstring. When I try inserting it into MySQL it returns an error for the column Binary(16) until I increase the size to Binary(36). That is essentially my issue yes I am not sure how to send Data to Node.js. – T Rogers Mar 13 '17 at 14:13
  • How do you sent the data, which format? I have no experience with node.js, you may have to convert the hex string back to 16 data bytes on the server side, before inserting in the database. – Martin R Mar 13 '17 at 14:16
  • No I am creating the UUID client side, sending it to a Node.js API an then onto CloudSQL (Google's Managed MySQL). Well I am using Alamofire for network requests so just as a String in the request parameters dictionary, I could not determine how to send non Strings. Apologies for the noob questions, but overloading trying to do back and frontend dev all at once. – T Rogers Mar 13 '17 at 14:20

0 Answers0