vendredi 20 février 2015

Why am i getting 3 bytes instead 1 byte after hexadecimal/string/byte conversion in java?

I have this program:



String hexadecimal = "AF";
byte decimal[] = new byte[hexadecimal.length()/2];

int j = 0;
for ( int i = 0; i < decimal.length; i++)
{
decimal[i] = (byte) Integer.parseInt(hexadecimal.substring(j,j+2),16); //Maybe the problem is this statement
j = j + 2;
}

String s = new String(decimal);
System.out.println("TOTAL LEN: " + s.length());

byte aux[] = s.getBytes();
System.out.println("TOTAL LEN: " + aux.length);


The first total is "1" and the second one is "3", i thought i would will get "1" in the second total. Why is happen this? My intention is generate another hexadecimal string with the same value as the original string (AF), but i am having this issue.


Regards!


P.D. Sorry for my english, let me know if i explained myself well.


Aucun commentaire:

Enregistrer un commentaire