<p>Here's something you can copy-paste in.</p>

<pre><code>byteArray = new Uint8Array([181,143,16,173,231,56,63,149,181,185,224,124,84,230,123,36]);
function toHexString(byteArray) {
  return Array.prototype.map.call(byteArray, function(byte) {
    return ('0' + (byte &amp; 0xFF).toString(16)).slice(-2);
  }).join('');
}
function toByteArray(hexString) {
  var result = [];
  for (var i = 0; i &lt; hexString.length; i += 2) {
    result.push(parseInt(hexString.substr(i, 2), 16));
  }
  return result;
}
hexString = toHexString(byteArray);
byteArray = toByteArray(hexString);
</code></pre>

<p>From <a href="https://stackoverflow.com/questions/14603205/how-to-convert-hex-string-into-a-bytes-array-and-a-bytes-array-in-the-hex-strin">here</a>/<a href="https://stackoverflow.com/questions/34309988/byte-array-to-hex-string-conversion-in-javascript">here</a>.</p>

CC BY-SA 3.0
2017-04-12T04:12:05.420Z
Using the above I'm getting a string that is not 128bits 289332005600940420022031 if I console log myByteArray i get an array buffer like "Uint8Array(16) [181, 143, 16, 173, 231, 56, 63, 149, 181, 185, 224, 124, 84, 230, 123, 36]" how do i convert this to an array?
Will-In-China
CC BY-SA 3.0
2017-04-12T04:19:41.583Z
@Will-In-China See edit.
Nick ODell
CC BY-SA 3.0
2018-04-12T15:55:04.920Z
toByteArray get wrong values when hex-string contains an odd number of characters: "fff", "1AB"
Geograph
CC BY-SA 4.0
2018-10-10T10:53:57.343Z
@Geograph since each byte is 2 hex digits, this is expected behaviour - a hex string with an odd number of digits is in invalid hex string.
david.barkhuizen
CC BY-SA 4.0
2017-04-12T03:50:56.473Z
2019-04-26T00:01:52.663Z
2019-04-26T00:01:52.663Z
Nick ODell
<p>I can generate a byte array with</p>

<pre><code>var myByteArray = window.crypto.getRandomValues(new Uint8Array(16))
</code></pre>

<p>and I get </p>

<pre><code>181,143,16,173,231,56,63,149,181,185,224,124,84,230,123,36
</code></pre>

<p>I can then turn this into a string with </p>

<pre><code>cryptoHelpers.convertByteArrayToString(myByteArray);
</code></pre>

<p>and i get </p>

<pre><code>µ­ç8?µ¹à|Tæ{$
</code></pre>

<p>But what I really want is a 128 bit hexadecimal like...</p>

<pre><code>6a3e52297b2e593f4d506f7164
</code></pre>

<p>And I want to be able to go back from hexadecimal to a byte array. </p>

<p>Is there a copy and paste function or library to make this magic happen?</p>

CC BY-SA 4.0
2018-10-17T18:15:19.680Z
You should ask this question on Stackoverflow, however search first.  I'm positive the answer will already exist.
Jestin
CC BY-SA 4.0
2018-10-20T09:53:53.403Z
I'm voting to close this question as off-topic because, the question, as written, does not appear related to bitcoin. Perhaps there is a relation, but if so, it should be made explicit.
dbkeys
CC BY-SA 4.0
2018-10-12T11:25:11.923Z
I'm voting to close this question as off-topic because it is unrelated to Bitcoin.
Claris
<cryptography><javascript>
Byte array to hexadecimal and back again in JavaScript
Will-In-China
# Byte array to hexadecimal and back again in JavaScript

(Asked by: username_0 on 2017-04-12)

I can generate a byte array with

```
var myByteArray = window.crypto.getRandomValues(new Uint8Array(16))

```

and I get

```
181,143,16,173,231,56,63,149,181,185,224,124,84,230,123,36

```

I can then turn this into a string with

```
cryptoHelpers.convertByteArrayToString(myByteArray);

```

and i get

```
µ­ç8?µ¹à|Tæ{$

```

But what I really want is a 128 bit hexadecimal like...

```
6a3e52297b2e593f4d506f7164

```

And I want to be able to go back from hexadecimal to a byte array.

Is there a copy and paste function or library to make this magic happen?

**Tags:** cryptography, javascript

> **Comments:**
> **username_3**: I'm voting to close this question as off-topic because it is unrelated to Bitcoin.  
> **username_1**: You should ask this question on Stackoverflow, however search first.  I'm positive the answer will already exist.  
> **username_2**: I'm voting to close this question as off-topic because, the question, as written, does not appear related to bitcoin. Perhaps there is a relation, but if so, it should be made explicit.  

---

## 1 answers

### Answer by username_4 on 2017-04-12. Score: 12 | Accepted answer

Here's something you can copy-paste in.

```
byteArray = new Uint8Array([181,143,16,173,231,56,63,149,181,185,224,124,84,230,123,36]);
function toHexString(byteArray) {
  return Array.prototype.map.call(byteArray, function(byte) {
    return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  }).join('');
}
function toByteArray(hexString) {
  var result = [];
  for (var i = 0; i < hexString.length; i += 2) {
    result.push(parseInt(hexString.substr(i, 2), 16));
  }
  return result;
}
hexString = toHexString(byteArray);
byteArray = toByteArray(hexString);

```

From [here](https://stackoverflow.com/questions/14603205/how-to-convert-hex-string-into-a-bytes-array-and-a-bytes-array-in-the-hex-strin)/[here](https://stackoverflow.com/questions/34309988/byte-array-to-hex-string-conversion-in-javascript).

> **Comments:**
> **username_0**: Using the above I'm getting a string that is not 128bits 289332005600940420022031 if I console log myByteArray i get an array buffer like "Uint8Array(16) [181, 143, 16, 173, 231, 56, 63, 149, 181, 185, 224, 124, 84, 230, 123, 36]" how do i convert this to an array?  
> **username_4**: @username_0 See edit.  
> **username_5**: toByteArray get wrong values when hex-string contains an odd number of characters: "fff", "1AB"  
> **username_6**: @username_5 since each byte is 2 hex digits, this is expected behaviour - a hex string with an odd number of digits is in invalid hex string.  

---

