John Davidson

Unable to convert Java getBytes() to php

0 comments
Message:


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)



My Answer:
"Hello World";_ byte[] bytes = a.getBytes();_ for (byte b : bytes) {_ System.out.print(b + " ");_ }_ }_}_PHP CODE_$a = "Hello World";_$bytes = unpack('C*', $a);_foreach ($bytes as $b) {_ echo $b . " ";_}_This PHP code should produce the same output as the Java code, converting the string "Hello World" to an array of bytes and printing each byte value.

Rate this post

4 of 5 based on 6172 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us