1

I'm currently developing an app where i need the string value "0xE3413F" to be converted to a UInt32 with same value, "0xE3413F". The reason for this is that I'm using the value in a function that requires an UInt32 value. I have saved the string in a .plist.

However, the only way I have found to do this is to first convert the string to Int and the to a UInt32, but this doesn't give me the original value.

Is there a way of converting a string directly to UInt32 or is there an other way to save UInt32 in a .plist?

Wain
  • 118,658
  • 15
  • 128
  • 151
ThomasGulli
  • 604
  • 1
  • 7
  • 20
  • 3
    plists can store numbers, why are you starting with a string? – Wain Mar 26 '15 at 17:20
  • Storing a number in the plist is probably the best solution. Otherwise this might help: http://stackoverflow.com/questions/26284223/how-to-convert-hex-number-to-bin-in-swift. – Martin R Mar 26 '15 at 17:25
  • @Wain ,because when I select "number" it automatically converts to 14 893 375, and not the value I would like, 0xE3413F – ThomasGulli Mar 26 '15 at 17:25
  • 5
    `0xE3413F` and `14893375` are just two different representations of the *same number* ... – Martin R Mar 26 '15 at 17:29
  • I see... I tried with var uint:UInt32 = UInt32(dict["\(randomNumber)"]![1] as Int!) , but that just gives me the int 14893375, not the 0xE3413F – ThomasGulli Mar 26 '15 at 17:41
  • 1
    @ThomasGulli You need to convert the integer back to a string if you want to view it as a hexadecimal, i.e. `let str = "0x" + String(14893375, radix: 16, uppercase: true) // 0xE3413F`. – Mick MacCallum Mar 26 '15 at 18:28

0 Answers0