Here is a useful method to convert binary to hexadecimal using Java.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
String HEX_STRING = "0123456789ABCDEF"; private static String convertBinary2Hexadecimal(byte[] binary) { StringBuffer buf = new StringBuffer(); int block = 0; for (int i=0; i<binary.length; i++) { block = binary[i] & 0xFF; buf.append(HEX_STRING.charAt(block >> 4)); buf.append(HEX_STRING.charAt(binary[i] & 0x0F)); } return buf.toString(); } |
Hello,
thanks for the tipp - but for what do you need that lines?
> byte[] data = new byte[binary.length * 2];
and
> boolean second = false;
Best wishes,
Rainer
true, those lines don’t have any purpose. removing them 😀
Im happy I located this blog, I couldnt locate any knowledge on this topic before. I also run a website and if you’re ever serious in a little bit of visitor writing for me please feel free to let me know, im always look for people to check out my website. Please stop by and leave a comment sometime!
how could i use this method…or calling this method if i dont know the byte[] binary value at this line
”
private static String convertBinary2Hexadecimal(byte[] binary) “
@ankgaa you should pass a byte array variable to that method when you call it.
i’m sorry
i’m still not understand,may u give an example to call that method by passing a byte array….
thx before
byte[] sample_var = …;
System.out.println(convertBinary2Hexadecimal(sample_var));
I tried this
String bin = “10010”;
byte[] binary = bin.getBytes();
debug(“Binary (convertBinary2Hexadecimal) = “+convertBinary2Hexadecimal(binary));
and the result was:
[DEBUG] Binary (convertBinary2Hexadecimal) = 3130303130
Are you sure this is correct or am I doing something wrong?
Dear All,
Please help to make code in java for Encryption and Decryption by using DES or 3DES Algorithms.
Best Regard,
Thank you.
here is a 3des class that i have used
http://tech.chitgoks.com/2012/04/11/tripledes-encrypter-class-in-java/
@Rich: This program works as designed. But it does not do the right work!
The output will ever be 3[0 or 1]*. So the program ist very funny but useless.