Java code To convert byte to Hexadecimal

后端 未结 19 2531
我寻月下人不归
我寻月下人不归 2020-11-22 17:26

I have an array of bytes. I want each byte String of that array to be converted to its corresponding hexadecimal values.

Is there any function in Java to convert a b

19条回答
  •  天命终不由人
    2020-11-22 17:41

    Others have covered the general case. But if you have a byte array of a known form, for example a MAC address, then you can:

    byte[] mac = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
    
    String str = String.format("%02X:%02X:%02X:%02X:%02X:%02X",
                               mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 
    

提交回复
热议问题