Results 1 to 6 of 6

Thread: [RESOLVED] Hex String

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Hex String

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

  3. #3
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    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("");

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: Hex String

    Quote Originally Posted by jpbro View Post
    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

  5. #5
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Hex String

    Quote Originally Posted by couttsj View Post
    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").

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,470

    Re: Hex String

    Quote Originally Posted by jpbro View Post
    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
  •  



Click Here to Expand Forum to Full Width