-
Jan 24th, 2022, 11:17 AM
#1
[RESOLVED] Hex String
Found this javascript code to generarate hex strings:
Code:
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
-
Jan 24th, 2022, 11:38 AM
#2
Re: Hex String
Try .toString(16).padStart(2, '0')
-
Jan 24th, 2022, 11:41 AM
#3
Re: Hex String
Or here's a neat one from StackOverflow:
Code:
"\tmyplaintext".split("")
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
.join("");
-
Jan 25th, 2022, 12:36 PM
#4
Re: Hex String
Originally Posted by jpbro
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.
Code:
<!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
-
Jan 25th, 2022, 12:47 PM
#5
Re: Hex String
Originally Posted by couttsj
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):
Code:
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").
-
Jan 25th, 2022, 03:10 PM
#6
Re: Hex String
Originally Posted by jpbro
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):
Code:
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").
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|