Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to base64 byte array in swift and java give different value

Incase of android everything is working perfectly. I want to implement same feature in iOS too but getting different values. Please check the description with images below.

In Java/Android Case:

I tried to convert the string to base64 byte array in java like

 byte[] data1 = Base64.decode(balance, Base64.DEFAULT);

Output: enter image description here

In Swift3/iOS Case:

I tried to convert the string to base64 byte array in swift like

let data:Data = Data(base64Encoded: balance, options: NSData.Base64DecodingOptions(rawValue: 0))!
let data1:Array = (data.bytes)

Output: enter image description here

like image 903
jazzbpn Avatar asked Dec 18 '17 09:12

jazzbpn


1 Answers

Finally solved:

This is due to signed and unsigned integer, meaning unsigned vs signed (so 0 to 255 and -127 to 128). Here, we need to convert the UInt8 array to Int8 array and therefore the problem will be solved.

let intArray = data1.map { Int8(bitPattern: $0) }
like image 169
jazzbpn Avatar answered Sep 28 '22 08:09

jazzbpn