0

I am attempting to convert the following Java code to PHP but I'm having issues with finding the equivalent of getBytes()

JAVA CODE

import java.math.BigInteger;

public class Test {
  public static void main(String[] ar){
     String a="4F23DE12";   
     BigInteger b = new BigInteger(a.getBytes());
     System.out.println(b); //Result is 3766753334112104754
  }
}

The Java result is 3766753334112104754 but my attempt with PHP is not giving the same result.

PHP CODE

    use phpseclib3\Math\BigInteger;

    $a="4F23DE12";
    $b = new BigInteger($a);
    echo $b; //Result is 4

The PHP result is 4. The issue seems to be from getBytes() and I have tried String to byte array in php and php equivalent of java getBytes() but still no solution.

In debugging it, I also tried System.out.println("4F23DE12".getBytes()); in Java, the result was [B@6d06d69c but I haven't found an exact equivalent in PHP.

Please I will appreciate any help on how to use PHP to get the same Java result (3766753334112104754)

Ayomikun Samuel
  • 125
  • 2
  • 9
  • 2
    You use `BigInteger($p)` but set `$a` – Nigel Ren Jun 06 '21 at 22:05
  • In Java, this `BigInteger b = new BigInteger(a.getBytes());` should be `BigInteger b = new BigInteger(a, 16);` (which is `1327750674` in decimal). – Elliott Frisch Jun 06 '21 at 22:33
  • @NigelRen was a mistake that I have corrected. Do you have any clue on the PHP equivalent? – Ayomikun Samuel Jun 06 '21 at 23:15
  • @ElliottFrisch The code I'm converting already uses getBytes(). So I need a solution with getBytes() – Ayomikun Samuel Jun 06 '21 at 23:18
  • Your PHP code is incorrect. The string you pass is clearly hexadecimal, but you're asking PHP to interpret it as a decimal string. So it only sees the characters that are valid in decimal, which is only the first character, "4". Use this instead: `$b = new BigInteger($a, 16);` and you get the same result 1327750674 as Elliot in his comment. – Erwin Bolwidt Jun 07 '21 at 00:02
  • @ErwinBolwidt Thanks. But how do I get **3766753334112104754** with PHP – Ayomikun Samuel Jun 07 '21 at 08:30
  • 1
    Using `.getBytes()` is the wrong thing to do. For one thing, you won't be able to reverse the operation, as there are many byte arrays that you cannot print as a String. And secondly, `.getBytes()` uses the default character encoding, and that depends on the OS that you run on and your settings. At the very least, do `.getBytes("UTF-8")` to get the same result on all platforms. With that disclaimer, it's simple to get the same in PHP as in Java (3766753334112104754): `$b = new BigInteger($a, 256);` - the "base 256" encoding in PHP interprets the characters of your string as bytes. – Erwin Bolwidt Jun 07 '21 at 09:01
  • @ErwinBolwidt Thanks for this, It worked. The .getBytes() code is lagacy code that am converting to PHP. So the PHP equivalent of `"4F23DE12".getBytes()` is `new BigInteger("4F23DE12", 256)` – Ayomikun Samuel Jun 07 '21 at 09:52

1 Answers1

0

My question was solved by @ErwinBolwidt. Check the comments for more info.

With @ErwinBolwidt solution, the PHP equivalent of "4F23DE12".getBytes() is new BigInteger("4F23DE12", 256).

Both will give you 3766753334112104754

Ayomikun Samuel
  • 125
  • 2
  • 9