I have two code in Java and Nodejs to convert int to byte array:
System.out.println((byte)254);
System.out.println(Integer.toHexString((byte)254));
-2
fffffffe
> parseInt(Buffer.from([254]).toString('hex'), 16)
254
> Buffer.from([254]).toString('hex')
'fe'
compare above 2 codes, I have some questions:
- why Java prints 254 as
-2
in byte but nodejs prints it as254
? - Java adds 6
f
in the prefix of hex string, but node only printsfe
. - Are these two byte same in the two lanugages?