The hex string is second most encoding to represent or transport binary data.
The answer is easy: each byte is converted into 2 characters (character set 0..9 and a..f) so this encoding doubles the size of data.
Especially when in debugging state of development phase the programmer wants to see what is in a variable and that can be better done than using a Base64 encoding or using decimals.
As a byte has a range of 256 numbers (decimal 0..255) the number is pushed into a 16 number base (compared to our decimal number space of 10) so we need to hexadecimal characters to get the value of one byte.
My implementations will provide the character in low or high chars, depending on the default of the framework.
This article focuses on the hex string encoding, if you like to know more about the Base64 representation of binary data see my article Base64 encoding & decoding.
This is a serious warning regarding the security of the programs shown in these article series. Always keep in mind my disclaimer regarding my programs: All programs are for educational purposes and are not intended to use in production or any other programs where a secure solution is needed. The programs do not have proper exceptional/error handling and in some cases they use insecure key lengths or other methods that are insecure. Never ever use the programs in real life unless checked by a qualified professional cryptographer.
The following links provide the solution in code and within an online compiler that runs the code. Kindly note that three articles use the same example: Base64 encoding & decoding, binary data to a hex string & back and SHA256 hashing.
*1) you need the external library pointycastle version 3.1.1
This is an output:
Generate a SHA-256 hash, Base64 en- and decoding and hex conversions
plaintext: The quick brown fox jumps over the lazy dog
sha256Value (hex) length: 32 data: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
sha256Value (base64): 16j7swfXgJRpypq8sAguT41WUeRtPNt2LQLQvzfJ5ZI=
sha256Base64 decoded to a byte array:
sha256Value (hex) length: 32 data: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
sha256HexString converted to a byte array:
sha256Value (hex) length: 32 data: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
Last update: Oct. 18th 2021
Back to the main page: readme.md