In: Computer Science
public static StringtoHexString (byte[] data ){/*behavior*/}
Translates data (RLE or raw) a hexadecimal string (without delimiters). This method can also aid debugging.
Ex:toHexString(new byte[] { 3, 15, 6, 4 }) yields string "3f64".
import java.util.Scanner;
public class StringtoHexString {
//method to return the string pf hexadecimals
public static String toHexString(byte[] data) {
String hexdec_num="";
//character array, at each index is the hex value of the index,
//index 15 has value f which is hex value of 15.
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
for(int i=0;i<data.length;i++){
//calculating hex value.
int value = data[i];
while(value>0)
{
int rem = value%16;
hexdec_num += hex[rem];
value /= 16;
}
}
return hexdec_num;
}
public static void main(String args[])
{
//calling function and printing value.
String res = toHexString(new byte[] { 3, 15, 6, 4 });
System.out.print("Hexadecimal number is : "+res+"\n");
}
}
output