1. Overview In the tutorial, we'll take a look at different ways to convert a byte array to a hexadecimal string, and vice versa. The record is introduced because the result of encryption and decryption will be a byte array, but my data must be passed through the HTTP protocol. 2. Converting Between Byte and Hexadecimal First of all, let's take a look at conversion logic between byte and hexadecimal numbers. 2.1 Byte to Hexadecimal The bytes are 8 bit signed integers in Java. Therefore, we need to convert each 4-bit segment to hex separately and concatenate them. Consequently, we'll get two hexadecimal characters after conversion. For instance, we can write 45 as 0010 1101 in binary, and the hexadecimal equivalent will be "2d": 0010 = 2 (base 10) = 2 (base 16) 1101 = 13 (base 10) = d (base 16) Therefore: 45 = 0010 1101 = 0x2d Let's implement the simple logic in Java: public String byteToHex(byte num) { char[] hexDigits = new char[2]...
留言
張貼留言