ArticleZip > How To Convert A Hexadecimal String To Uint8array And Back In Javascript

How To Convert A Hexadecimal String To Uint8array And Back In Javascript

Have you ever needed to convert a hexadecimal string to a Uint8Array in JavaScript and vice versa? It can be a handy skill to have in your coding toolkit. In this article, we will walk you through the process step by step, making it easy to understand and implement.

**Converting Hexadecimal String to Uint8Array:**
To convert a hexadecimal string to a Uint8Array, you will first need to create a function that takes the hexadecimal string as input. The following code snippet demonstrates how this function can be done:

Javascript

function hexStringToUint8Array(hexString) {
  const hex = hexString.match(/.{1,2}/g);
  return new Uint8Array(hex.map(byte => parseInt(byte, 16)));
}

In the function `hexStringToUint8Array()`, we first use the `.match()` method to split the hexadecimal string into an array of two characters each. Then, we use the `.map()` method to iterate over each pair of characters and convert them to integers using `parseInt(value, 16)`. Finally, we create a new Uint8Array from the resulting array of integers.

**Converting Uint8Array to Hexadecimal String:**
Converting a Uint8Array back to a hexadecimal string is also straightforward. The following code snippet demonstrates a function that accomplishes this:

Javascript

function uint8ArrayToHexString(uint8Array) {
  return uint8Array.reduce((hexString, byte) => hexString + byte.toString(16).padStart(2, '0'), '');
}

In the function `uint8ArrayToHexString()`, we use the `.reduce()` method to iterate over each byte in the Uint8Array. For each byte, we convert it to a hexadecimal string using `.toString(16)` and ensure it is padded to two characters using `.padStart(2, '0')`. The resulting hexadecimal strings are then concatenated to form the final hexadecimal string representation.

**Putting It All Together:**
Now that we have the functions to convert a hexadecimal string to a Uint8Array and vice versa, we can easily utilize them in our JavaScript code. Here is an example of how you can convert between the two formats:

Javascript

const hexString = '546869732069732061207465737420737472696e67';
const uint8Array = hexStringToUint8Array(hexString);
console.log(uint8Array);

const newHexString = uint8ArrayToHexString(uint8Array);
console.log(newHexString);

In this example, we first convert the `hexString` to a `Uint8Array` using the `hexStringToUint8Array()` function and then convert it back to a hexadecimal string using the `uint8ArrayToHexString()` function. The resulting `Uint8Array` and hexadecimal string are then logged to the console for verification.

**Conclusion:**
Converting a hexadecimal string to a Uint8Array and back in JavaScript is a useful skill to have when working with binary data. By following the straightforward steps outlined in this article, you can easily perform these conversions in your projects. So, next time you encounter a need for these conversions, you'll be well-equipped to handle them efficiently.