Android Question DecodeBase64 compatibility with PHP

Dario126

Member
Licensed User
Longtime User
I have different result while decoding base_64 string with PHP or B4A.

Source string (inside question marks) is "gaCpU6ikVpqqsqGmcg=="

With PHP base64_decode() function I get this " ©S¨¤Všª²¡¦r"

And with B4A code
B4X:
sData="gaCpU6ikVpqqsqGmcg=="

Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64(sData)
sData = BytesToString(b, 0, b.Length, "UTF8")
   
Log("Base_64 decoded: " & sData)

i get this result "���S��V�����r"

I suspect that there is some difference in Charsets. Is it?
How to make B4A result compatible with PHP result?
 

Linostar

Member
Licensed User
Longtime User
It is the same string for both. It just seems that B4A's log window wasn't able to display non-ASCII characters properly. That wouldn't matter if you're only going to use those strings for comparison and/or storage. For displaying on B4A's UI views, you may have this problem (perhaps changing the font to another that supports unicode chars would solve this), but that depends whether or not you intend to display binary strings in the first place.
 
Upvote 0

Dario126

Member
Licensed User
Longtime User
Something else must be wrong..

I made this test:
B4X:
sData="eaGqVKmldg=="

    'LIST KEYCODES BEFORE DECODING
    Log("Source data: " & sData)
    Log("ASC codes")
    For i = 0 To sData.Length-1
        sChar=sData.SubString2(i,i+1)
        Log(Asc(sChar))
    Next

    'BASE_64 DECODE
    Dim su As StringUtils
    Dim b() As Byte = su.DecodeBase64(sData)
    sData = BytesToString(b, 0, b.Length, "UTF8")
    Log("Base_64 decoded: " & sData)

    'LIST KEYCODES AFTER DECODING
    Log("ASC codes")
    For i = 0 To sData.Length-1
        sChar=sData.SubString2(i,i+1)
        Log(Asc(sChar))
    Next

and I get this result:

Source data: eaGqVKmldg==
ASC codes
101
97
71
113
86
75
109
108
100
103
61
61
Base_64 decoded: y��T��v
ASC codes
121
65533
65533
84
65533
65533
118

So after base_64 decoding some bytes are changed to very high numbers.

Original string before coding have those ASC codes: 121, 161, 170, 84, 169, 165, 118 .. and through PHP decode I get it right back, but through B4A decode I get ASCI as above..

:confused::confused::confused:
 
Upvote 0

Linostar

Member
Licensed User
Longtime User
Something else must be wrong..

I made this test:
B4X:
sData="eaGqVKmldg=="

    'LIST KEYCODES BEFORE DECODING
    Log("Source data: " & sData)
    Log("ASC codes")
    For i = 0 To sData.Length-1
        sChar=sData.SubString2(i,i+1)
        Log(Asc(sChar))
    Next

    'BASE_64 DECODE
    Dim su As StringUtils
    Dim b() As Byte = su.DecodeBase64(sData)
    sData = BytesToString(b, 0, b.Length, "UTF8")
    Log("Base_64 decoded: " & sData)

    'LIST KEYCODES AFTER DECODING
    Log("ASC codes")
    For i = 0 To sData.Length-1
        sChar=sData.SubString2(i,i+1)
        Log(Asc(sChar))
    Next

and I get this result:



So after base_64 decoding some bytes are changed to very high numbers.

Original string before coding have those ASC codes: 121, 161, 170, 84, 169, 165, 118 .. and through PHP decode I get it right back, but through B4A decode I get ASCI as above..

:confused::confused::confused:

That's because you are using UTF8 encoding, which encodes sometimes on 8 bits, sometimes on 16 bits, and sometimes on more, depending on the character. Use other encodings in B4A, that encode on 8 bits to obtain the same results as PHP, such as "CP1252" and "ISO8859-1".
Thus, your code will become:

B4X:
sData = BytesToString(b, 0, b.Length, "ISO8859-1")

Otherwise, use UTF-8 encoding in PHP through utf8_encode() function.
 
Upvote 0

Dario126

Member
Licensed User
Longtime User
I see now the difference. Thank You.
Maybe You can help me a little bit more.

I'm newbie with PHP, and can't figure out which coding setup is on my server. Is it possible to readout so I use same coding in B4A?

I tried some of most common (google result) but always get different result then base64_decode from server ..
 
Upvote 0

Linostar

Member
Licensed User
Longtime User
PHP uses ASCII code by default if I'm not mistaken, which is compatible with Windows and ISO coding (CP1252 and ISO 8859-1).

What encoding you choose depends on your needs. What language(s) are you putting in your strings? If it's only English, you can stay with the default coding in PHP, and use "ISO8859-1" in your B4A code. If there are more than one language that do not share the same family, you'd better choose a unicode coding (such as UTF-8), and use utf8_encode() function in PHP to transform your strings.

Since unicode is much more used nowadays (it's well suited when you use multiple languages), it is natural that you'll find in google unicode-encoded results that differ from the ASCII-encoded results of the PHP server.

Hope this helps.
 
Upvote 0

Dario126

Member
Licensed User
Longtime User
It helps, and saves me probably quite much time (which I don't have at moment) to investigate this. So thank You very much ..

Btw. CP1252 is not fully compatible (some characters are also transformed to large numbers), but ISO8859-1 return same result from PHP server and B4A ..

I'm not sure if I would go with unicode right now, because it's less "readable" and not sure if I can copy-paste results (coded string) in all cases. I think I'll stick with ISO for the moment ..

I'm using this for transport of crypted string through JSON response from server. There is no special language used here, but keycodes goes up to 255 and copy-paste between some programs missing some characters (not sure what happens if saving to file), so to be on safe side I pass it through base64_encode/decode ..
 
Upvote 0
Top