0

I'm trying to use a service which "base64 converts" their data which they then push to my server. The data is divided into three data types: strings, integers and booleans. Why they package it this way I don't understand, but it's up to me to decipher it.

I have a string Qjo0MDk2 which should convert to B:4096 and PHP's native base64_decode function works!

However, if I try to convert AAATmg== to a base-10 (decimal) integer value, I want to get 5018, but base64_decode just gives me nothing. (I assume because it's trying to convert to a string, rather than a base-10 integer.)

Likewise, AA== should convert to 0 in base-2 (binary) boolean values, while AQ== should convert to 1 in the same.

Is there a set of functions that does this already somewhere? I can't imagine this is new. Here is a website that does it today, but the code is not exposed: https://conv.darkbyte.ru/

6
  • 1
    base64_decode does give you something but it's binary, so if you dump it, it's just a character there is no actual character for (= the symbol with the question mark in it). There are functions to convert binary to decimal (and hex), tests show that I get 0 if I try bindec. It actually works if I go first to hexadecimal hexdec(bin2hex(base64_decode(...))). Commented Dec 13, 2016 at 20:47
  • Just a heads up, the encoded value of 5018 appears to be NTAxOA== not AAATmg==. Commented Dec 13, 2016 at 20:49
  • 1
    @CharlotteDunois That works for decimal/integer, thank you! But then why doesn't this work for binary? It always prints true when AA== should give 0 and AQ== should give 1. Code: if(base64_decode("AA==")) echo "true"; else echo "false"; Commented Dec 13, 2016 at 20:53
  • 1
    Here's why: 3v4l.org/4C9l4 3v4l.org/sEXtd Commented Dec 13, 2016 at 20:55
  • 1
    @CharlotteDunois Thank you so much! I'd been beating my head against this for several days! If you submit as an answer I'll happily accept it. Commented Dec 13, 2016 at 20:58

1 Answer 1

4

Binary data can't be simply printed, that's why you don't see something from base64_decode, but the data is there. If you want to actually see something, you need to convert the data to hexadecimal (technically into a hexadecimal representation). Since it looks like that the third party application is doing that (though in the other direction), you will have to do that for all data.

The data representation for AA== and AQ== is the same when you use them in an if statement, even though they are 00 and 01 hexadecimal-wise. They're true-ish to PHP, thus executing the if part. If you actually want to check their state, you will have to convert them to int (from the hexadecimal representation).

(int) bin2hex(base64_decode("AA==")) // int(0)
(int) bin2hex(base64_decode("AQ==")) // int(1)

Trying to convert from binary to int directly will result in int(0). So you have to be cautious when you deal with data from the third party application.

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

Comments

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.