# Other Languages > XML, HTML, Javascript, Web and CSS >  [RESOLVED] Hex String

## couttsj

Found this javascript code to generarate hex strings:


```
function StrToHex(str)
{
  var arr1 = [];
  for (var n = 0, l = str.length; n < l; n ++){
    var hex = Number("0"+str.charCodeAt(n)).toString(16);
    arr1.push(hex);}
  return arr1.join('');
}
```

It works great if the string is ASCII, but it drops the leading "0" on single digit characters such as "0f". How do I fix this?

J.A. Coutts

----------


## jpbro

Try .toString(16).padStart(2, '0')

----------


## jpbro

Or here's a neat one from StackOverflow:



```
"\tmyplaintext".split("")
     .map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
     .join("");
```

----------


## couttsj

> Try .toString(16).padStart(2, '0')


That doesn't work. As a matter of fact, none of the suggestions I found work because I am using an ordinary array. Apparently, to get the non-character bytes to convert properly, I have to use a typed array. If I declare the array as a Uint8Array this works.


```
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
</head>
<body>
<h2>HEX Test</h2>
<PRE>
<script>
bArray = new Uint8Array(16);
bArray = [10,2,30,4,50,6,70,8,90,10,110,12,130,14,150,16];
document.writeln(bArray);
hexString = toHexString(bArray);
document.writeln(hexString);
newArray = toByteArray(hexString);
document.writeln(newArray);

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;}
```

Now all I have to do is figure out how to move an ordinary array to a typed array.

J.A. Coutts

----------


## jpbro

> It works great if the string is ASCII, but it drops the leading "0" on single digit characters such as "0f". How do I fix this?


Does the original code you provided work great (except for leading zeros) or not? If so, using padStart should do the trick (it does here):



```
function StrToHex(str)
{
  var arr1 = [];
  for (var n = 0, l = str.length; n < l; n ++){
    var hex = Number("0"+str.charCodeAt(n)).toString(16).padStart(2, '0');
    arr1.push(hex);}
  return arr1.join('');
}
```

For me, calling StrToHex('\tabc') produces: "09414243"

(Tab is being correctly padded to "09").

----------


## couttsj

> Does the original code you provided work great (except for leading zeros) or not? If so, using padStart should do the trick (it does here):
> 
> 
> 
> ```
> function StrToHex(str)
> {
>   var arr1 = [];
>   for (var n = 0, l = str.length; n < l; n ++){
> ...


I must have screwed something up before, because it seems to work now.
Before:
Text: :38522:TZ10200252.-3011-
3a33383532323a545a31303230303235322e2d333031312d
c148c015f12f726a4c971d2f3f3314d6d64522cd1f55ba9f
37e3bd7160fe22cecbfdfd59ad12e2fc73dba6f89e78c

After:
Text: :38522:TZ10200252.-3011-
3a33383532323a545a31303230303235322e2d333031312d
c148c015f12f726a4c971d2f3f3314d6d64522cd1f55ba9f
37e3bd7160fe22cecbfdfd59ad012e2fc73dba060f89e78c

Thank you;
J.A.Coutts

----------

