mercredi 5 mars 2014

String to Byte[] and Byte to String - JAVA


Vote count:

0




Given the following example:



String f="FF00000000000000";
byte[] bytes = DatatypeConverter.parseHexBinary(f);
String f2= new String (bytes);


i want the output to be FF00000000000000 but it's not working with this method , any help please ?



asked 4 mins ago


3 Answers



Vote count:

0




You're currently trying to interpret the bytes as if they were text encoded using the platform default encoding (UTF-8, ISO-8859-1 or whatever). That's not what you actually want to do at all - you want to convert it back to hex.


For that, just look at the converter you're using for the parsing step, and look for similar methods which work in the opposite direction. In this case, you want printHexBinary :



String f2 = DatatypeConverter.printHexBinary(bytes);


answered 1 min ago



Vote count:

0




Try something like this:



String s = Byte.toString((byte)0x63) ;


answered 1 min ago



Vote count:

-1




Why you are using DatatypeConverter?



String f="FF00000000000000";

String f2= new String (f.getBytes());
System.out.println(f2.toString());


answered 59 secs ago





Aucun commentaire:

Enregistrer un commentaire