You are missing the padding in the hex conversion. You'll want to use

function toHexString(byteArray) {
  return Array.from(byteArray, function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('')
}

so that each byte transforms to exactly two hex digits. Your expected output would be 04812d7e3a9829e5d51bdd64ceb35df060699bc1309731bd6e6f1a5443a7f9ce0af4382fcfd6f5f8a08bb2619709c2d49fb771601770f2c267985af2754e1f8cf9

Answer from Bergi on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › toHex
Uint8Array.prototype.toHex() - JavaScript - MDN Web Docs
The toHex() method of Uint8Array instances returns a hex-encoded string based on the data in this Uint8Array object. This method creates strings from a byte array.
Discussions

javascript - How to convert hex string into a bytes array, and a bytes array in the hex string? - Stack Overflow
The following code procedure bytes = parseHexString (createHexString (bytes)) leads to updated of bytes, what I would like to avoid. And as a result calculations are not correct. More on stackoverflow.com
🌐 stackoverflow.com
cryptography - Byte array to hexadecimal and back again in JavaScript - Bitcoin Stack Exchange
And I want to be able to go back from hexadecimal to a byte array. More on bitcoin.stackexchange.com
🌐 bitcoin.stackexchange.com
javascript - How to convert byte array to hex string? - Stack Overflow
With "wrong" do you mean the JavaScript code or something in the Python code (in the latter case, explain what you expect instead)? ... The output shown (both variants) are similar. They are not however identical. What do you actually want? ... Your Python code is correct (although it can be written much more concise as the other answers suggest), but your JS code isn't because it clearly outputs a hex string that is not the same as the input. ... function toHexString(bytes... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Converting a hex string into a BYTE array JS - Stack Overflow
I have been at this for a bit, and I am new to programing with JS. I am making a game using JS, HTML5, node and socket.io. I am working on the protocol right now and I am sending the server strings More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › fromHex
Uint8Array.fromHex() - JavaScript - MDN Web Docs
July 10, 2025 - The string must: Have an even number of characters because two characters encode one byte. Only contain characters in the hexadecimal alphabet, which includes 0–9 and A–F (case-insensitive). Not contain whitespace (unlike Uint8Array.prototype.setFromBase64()). A new Uint8Array object containing the decoded bytes from the hexadecimal string.
Top answer
1 of 7
86

Convert a hex string to a byte array and vice versa

note: implementation from crypto-js, though now out of date and slightly altered

// Convert a hex string to a byte array
function hexToBytes(hex) {
    let bytes = [];
    for (let c = 0; c < hex.length; c += 2)
        bytes.push(parseInt(hex.substr(c, 2), 16));
    return bytes;
}

// Convert a byte array to a hex string
function bytesToHex(bytes) {
    let hex = [];
    for (let i = 0; i < bytes.length; i++) {
        let current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
        hex.push((current >>> 4).toString(16));
        hex.push((current & 0xF).toString(16));
    }
    return hex.join("");
}
2 of 7
14

Update: Scroll down for solution... Live Demo

The issue: you are using a lossy conversion to hex, which cannot be reversed.

var p = parseHexString(createHexString(rsa_privk[0]));

This will never be same as rsa_privk[0].

Because, createHexString() only uses the last 2 bytes from each array element.

Example:

rsa_privk[0] : [123676133, 198914513, 129998601, 245147334, 11918451, 206998232, 96766191, 75984899, 177840095, 106709334, 10180427, 208237547, 119814814, 127003446, 189062377, 84099480, 220452154, 250519075, 267883908, 115471915, 165124106, 238628722, 169382478, 42320122, 95982405, 80725759, 89608310, 85166267, 200925925, 254033325, 86971506, 191278317, 127411298, 180195794, 142776693, 188738169, 39016]

createHexString(rsa_privk[0]) : e5d109c673d8ef03df564beb9e36e9983a23842b0a724efa45ff76bbe5ad72ed62d2757968

parseHexString(createHexString(rsa_privk[0])) : [229, 209, 9, 198, 115, 216, 239, 3, 223, 86, 75, 235, 158, 54, 233, 152, 58, 35, 132, 43, 10, 114, 78, 250, 69, 255, 118, 187, 229, 173, 114, 237, 98, 210, 117, 121, 104] 

Update : Working Solution...

The two functions... the hex always contains 8 byte blocks, each for each element in the array...

function parseHexString(str) { 
    var result = [];
    while (str.length >= 8) { 
        result.push(parseInt(str.substring(0, 8), 16));

        str = str.substring(8, str.length);
    }

    return result;
}

function createHexString(arr) {
    var result = "";
    var z;

    for (var i = 0; i < arr.length; i++) {
        var str = arr[i].toString(16);

        z = 8 - str.length + 1;
        str = Array(z).join("0") + str;

        result += str;
    }

    return result;
}

Test code...

function test() {   
    a = [123676133, 198914513, 129998601, 245147334, 11918451, 206998232, 96766191, 75984899, 177840095, 106709334, 10180427, 208237547, 119814814, 127003446, 189062377, 84099480, 220452154, 250519075, 267883908, 115471915, 165124106, 238628722, 169382478, 42320122, 95982405, 80725759, 89608310, 85166267, 200925925, 254033325, 86971506, 191278317, 127411298, 180195794, 142776693, 188738169, 39016];

    console.log("Input");
    console.log(a);

    b = createHexString(a);

    console.log("Hex");
    console.log(b);

    c = parseHexString(b); 

    console.log("Output");
    console.log(c);

    if(checkIfEqual(a, c)) {
        alert("Same");
    }
}

function checkIfEqual(arr1, arr2) {
    if (arr1.length != arr2.length) {
        return false;
    }
    //sort them first, then join them and just compare the strings
    return arr1.sort().join() == arr2.sort().join();
}
🌐
Sling Academy
slingacademy.com › article › javascript-convert-a-byte-array-to-a-hex-string-and-vice-versa
JavaScript: Convert a byte array to a hex string and vice versa - Sling Academy
March 13, 2023 - This concise and straightforward article shows you how to convert a byte array into a hex string (hexadecimal string) and vice versa in modern JavaScript (ES6 and above). Overview What are hex strings and byte arrays? A hex...
🌐
Programming Idioms
programming-idioms.org › idiom › 175 › bytes-to-hex-string › 4739 › js
Bytes to hex string, in JS
December 16, 2018 - for i,v in ipairs(a) do a[i]=string.format("x",v) end local s=table.concat(a) ... fn byte_to_hex(byte: u8) -> (u8, u8) { static HEX_LUT: [u8; 16] = [b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'a', b'b', b'c', b'd', b'e', b'f']; let upper = HEX_LUT[(byte >> 4) as usize]; let lower = HEX_LUT[(byte & 0xF) as usize]; (lower, upper) } let utf8_bytes: Vec<u8> = a.iter().copied().flat_map(|byte| { let (lower, upper) = byte_to_hex(byte); [upper, lower] }).collect(); let s = unsafe { String::from_utf8_unchecked(utf8_bytes) };
Find elsewhere
🌐
Java2s
java2s.com › example › nodejs › string › convert-byte-array-to-hex-string.html
Convert byte Array To Hex String - Node.js String
function byteArrayToHexString(byteArray) { var hexString = ''; var nextHexByte; for (var i=0; i<byteArray.byteLength; i++) { nextHexByte = byteArray[i].toString(16); // Integer to base 16 if (nextHexByte.length < 2) { nextHexByte = "0" + nextHexByte; // Otherwise 10 becomes just a instead of ...
🌐
GitHub
gist.github.com › llzes › 7e0a324239653f32f8eb78a09208de52
Byte to Hex and Hex to Byte in JavaScript. · GitHub
Byte to Hex and Hex to Byte in JavaScript. GitHub Gist: instantly share code, notes, and snippets.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › setFromHex
Uint8Array.prototype.setFromHex() - JavaScript | MDN
Have an even number of characters because two characters encode one byte. Only contain characters in the hexadecimal alphabet, which includes 0–9 and A–F (case-insensitive). Not contain whitespace (unlike Uint8Array.prototype.setFromBase64()). Note that the string is only read up to the point where the array is filled, so any invalid hex syntax after that point is ignored.
🌐
Webdevtutor
webdevtutor.net › blog › javascript-byte-array-to-hex-string
Converting JavaScript Byte Array to Hex String
You can use this byteArrayToHexString function to convert any byte array to a hex string in JavaScript easily.
🌐
LiveJournal
conjuncte.livejournal.com › 191738.html
JS: Byte array to HEX string: conjuncte — LiveJournal
June 21, 2012 - В процессе работы получился такой кусочек кода - перевод массива байт в hex-строку. function toHexString ( bytes ) { return bytes . map ( function ( byte ) { return ( " 00 " + ( byte 0xFF ) . toString ( 16 ) ) . slice ( - 2 ) } ) . join ( ' ' ) } Например, var hex = toHexString([10, 16, -10,…
🌐
cyberangles
cyberangles.org › blog › byte-array-to-hex-string-conversion-in-javascript
Byte Array to Hex String Conversion in JavaScript: How to Ensure Correct and Efficient Results — CyberAngles.org
In JavaScript: Browsers use Uint8Array (a typed array) to represent byte arrays. Node.js historically uses Buffer (a built-in class optimized for binary data), though Uint8Array is also supported.
🌐
Prussiafan
prussiafan.club › posts › hex-to-bytes-and-back
Hex to Bytes and Back - prussiafan.club
September 15, 2023 - The 8 stands for the 8 bits, so a Uint8 is an array of one byte unsigned integers^\[2\]^. Basically, it's a way to represent bytes in Javascript by storing in as an array of decimal numbers from 0-255. ... The loop iterates through through the `Uint8Array`, first dividing it by 16 and rounding ...
🌐
Jsben
jsben.ch › convert-array-of-bytes-to-hex-string-vkpag
Convert array of bytes to hex string - JSBEN.CH JavaScript Benchmark
The test evaluates different implementations of converting an array of byte values to a hexadecimal string. It examines variations in parameter handling and err