0

I have to convert an existing Java code for encryption to PHP. I am stuck in the below Java code where the key is converted to a byte array using Java's BigInteger class

private static byte[] stringToBytes(String str){ 

    byte[] bytes=new BigInteger(str, Character.MAX_RADIX).toByteArray();
    return Arrays.copyOfRange(bytes, 1, bytes.length); 
} 

I am able to convert the input string to big int using

base_convert($key,36,10);

But cannot convert to byte array. Any inputs please

A sample output for Java code is shown below

stringToBytes("TestString")

gives

[10, -100, -89, -107, 122, -108, 44]

Raj
  • 1
  • 1
  • 1
    Previously answered in this thread. http://stackoverflow.com/questions/885597/string-to-byte-array-in-php – greg_diesel Feb 13 '15 at 21:07
  • I tried unpack as mentioned in that question. However I could not find any combination which returns the same byte array as returned by Java's BigInteger.toByteArray() method – Raj Feb 13 '15 at 21:15
  • They're two very different languages. In PHP you might not even need a byte array because of the loose typing of PHP. By the way, what did you use as PHP's equivalent to Java's `BigInteger` class? – developerwjk Feb 13 '15 at 21:32
  • In PHP when you use base_convert($key,36,10) does that give you the same Big Int as you get in Java? – greg_diesel Feb 13 '15 at 21:50
  • base_convert does not give the same big int as in Java because of precision issue for higher number. so i used a different version posted by someone. So big int returned by java and php are same now. But the issue is converting it to byte array format that Java's Big int uses – Raj Feb 13 '15 at 22:06

0 Answers0