Hex Your Bytes to Display

Hex Your Bytes to Display

Editor’s Note:This text has changed from the original version.

Have you ever needed toconvert a byte array (byte []) into a displayable hex string? The JDK does notprovide this capability for byte arrays, only for the wrapper classes.

Below is the code which will convert a byte array into a displayable “hex” representation of the string.

Now you can turn

 byte b[] = {0xf1, 0x0d,  0x3c, 0x44};

into:

 String s =  "F10D3C44"

like this:

 String s =  byteArrayToHexString(b);
  • provides the same functionality found in Integer.toHexString(), Short.toHexString(), etc…
  • Simple to use
  • Fast, efficient and accurate
  • Tested and proven

Here’s the code:

 /**
* Convert a byte[] array to readable string format. This makes the "hex"readable!
* @return result String buffer in String format
* @param in byte[] buffer to convert to string format
*/
static String byteArrayToHexString(byte in[]) {
byte ch = 0x00;
int i = 0;
if (in == null || in.length <= 0)
return null;

String pseudo[] = {"0", "1", "2","3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E","F"};
StringBuffer out = new StringBuffer(in.length * 2);

while (i < in.length) {
ch = (byte) (in[i] & 0xF0); // Strip offhigh nibble
ch = (byte) (ch >>> 4); // shift the bits down
ch = (byte) (ch & 0x0F); // must do this is high order bit is on!
out.append(pseudo[ (int) ch]); // convert thenibble to a String Character
ch = (byte) (in[i] & 0x0F); // Strip offlow nibble
out.append(pseudo[ (int) ch]); // convert thenibble to a String Character
i++;
}
String rslt = new String(out);
return rslt;
}

Stay tuned, I’ll present some additionalroutines which take the “hex” data and format it into a “Hex Dump”format like: 0000: 32 00 00 00 00 49 99 04 13 14 56 20 17 552B 3F ____[2 I

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as