0

I would like to convert a base64 string to other types, ie integer, float, and char array.

In Java I can use a ByteBuffer object, so it's easy to get values with methods like stream.getInt(), stream.getLong(), stream.getChar(), stream.getFloat(), etc.

But how to do this in PHP?

Edit: I tried the base64-decode PHP function, but this one decode a string, I want to decode numbers.

Edit2:

In Java:

import java.nio.ByteBuffer;
import javax.xml.bind.DatatypeConverter;

public class Main {

    public static void main(String[] args) {
        ByteBuffer stream = ByteBuffer.wrap(DatatypeConverter.parseBase64Binary("AAGdQw=="));
        while (stream.hasRemaining()) {
            System.out.println(stream.getInt());
        }
    }
}

displays 105795

But in php:

$nb = base64_decode('AAGdQw=='); // the result is not a stringified number, neither printable
settype($nb, 'int');
echo $nb;

displays 0, because base64_decode('AAGdQw==') is not printable.

4
  • 3
    have you tried anything so far? perhaps reading the manual, especially about base64_decode and type juggling? Commented Nov 18, 2016 at 10:22
  • Of course I tried base64_decode, but I don't want to decode a string, I want also to decode integers and floats. Commented Nov 18, 2016 at 10:23
  • that's why i linked to the page about type juggling. Commented Nov 18, 2016 at 10:39
  • And I don't want to cast "64" to 64. Please see my edit. Commented Nov 18, 2016 at 11:29

2 Answers 2

4

you have to use unpack to convert the decoded string into a byte array, from which you then can reconstruct the integer:

<?php
$s = 'AAGdQw==';
$dec = base64_decode($s);
$ar = unpack("C*", $dec);
$i= ($ar[1]<<24) + ($ar[2]<<16) + ($ar[3]<<8) + $ar[4];

var_dump($i);
//output int(105795)

floats could be re-assembled with the pack-function, which creates a variable from a byte array.

but be advised that you have to take a lot of care about both data types AND the underlying hardware; especially the endianness (byte order) of the processor and the word size of the processor (32bit int differs from 64bit int) - therefore, if possible, you should use a text-based protocol, like JSON - which you can base64-encode, too.

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

Comments

0

Decode a content as is and cast a result to any type through type casting or some varialbe handling function such as settype().

If encoded file is large and you worried about memory consumption, you can use stream filters (relevant answer).

3 Comments

Thank you. I tried but I don't understand why settype(base64_decode('AAGdQw=='), 'int') returns 1.
settype() accepts argument by reference (see the manual), so you need to assign decoding result to a variable and apply settype. Also you can use intval().
Works better. The fact is my base64 string do not contains a stringified integer, it contains the integer itself: ie AAGdQw== is supposed to be 105795, but the decoded string is not printable, so I don't think I can use it with intval(). See my edit.

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.