dcsimg
www.webdeveloper.com
Results 1 to 7 of 7

Thread: Converting Hex Strings

  1. #1
    Join Date
    Jan 2008
    Posts
    123

    Converting Hex Strings

    All,

    I'm looking for a method of converting hex strings to HTML in Javascript.

    I essentially need this: http://chxo.com/scripts/hex2string.php

    but I need it to work locally.

    Does anyone out there know of a Javascript framework, external file, or a way for me to achieve this?

    Thank you all,

    --thesprucegoose

  2. #2
    Join Date
    Jan 2008
    Posts
    123
    I have slightly more information now.

    I'm going to get a string who's delimiter is an ampersand:

    photoFile3=pic4.jpg&
    photoCaptionID3=13041&
    photoCaption3=53756e666c6f7765722053746172&
    photoDepth3=9&
    photoSpecies3=13041&

    In the case of photoCaption3, I need to take that HEX and convert it into a text string

    In the case of photoCaptionID3, I have a separate file (that's an Excel sheet right now) that has the IDs. For example, 13041 is a Sunflower Starfish. I need a way to take that number and make it dynamically tell the HTML file that 13041 means Sunflower Starfish.

    Any help would be appreciated greatly. I'm going to continue to work on this and check back periodically.

    Thanks again,

    --thesprucegoose

  3. #3
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,843

    Thumbs up

    Quote Originally Posted by thesprucegoose View Post
    All,

    I'm looking for a method of converting hex strings to HTML in Javascript.

    I essentially need this: http://chxo.com/scripts/hex2string.php

    but I need it to work locally.

    Does anyone out there know of a Javascript framework, external file, or a way for me to achieve this?

    Thank you all,

    --thesprucegoose
    See if this works for you:
    Code:
    <html>
    <head>
    <title>Hex Conversions</title>
    <script type="text/javascript">
    function d2h(d) {return d.toString(16);}
    function h2d(h) {return parseInt(h,16);} 
    
    function Str2Hex() {
      var tmp = document.getElementById('strSource').value;
      var str = '';
      for (var i=0; i<tmp.length; i++) {
        c = tmp.charCodeAt(i);
        str += d2h(c) + ' ';
      }
      document.getElementById('hexSource').value = str;
    }
    function Hex2Str() {
      var tmp = document.getElementById('hexSource').value;
      var arr = tmp.split(' ');
      var str = '';
      for (var i=0; i<arr.length; i++) {
        c = String.fromCharCode(h2d(arr[i]));
        str += c;
      }
      document.getElementById('strOutput').value = str;
    }
    </script>
    </head>
    <body>
    Input String:
    <br>
    
    <textarea id="strSource" rows="5" cols="40">
    Now is the time
    for all good men
    to come.
    </textarea>
    <!-- Sample input above -->
    
    <p>
    <button onclick="Str2Hex()">Convert to</button>
    <br>
    Hex String:
    <br>
    <textarea id="hexSource" rows="5" cols="40"></textarea>
    <p>
    <button onclick="Hex2Str()">Convert to</button>
    <br>
    Output String:
    <br>
    <textarea id="strOutput" rows="5" cols="40"></textarea>
    </body>
    </html>

  4. #4
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,843

    Lightbulb

    See comments below:


    Quote Originally Posted by thesprucegoose View Post
    I have slightly more information now.

    In the case of photoCaption3, I need to take that HEX and convert it into a text string
    See Post #3


    In the case of photoCaptionID3, I have a separate file (that's an Excel sheet right now) that has the IDs. For example, 13041 is a Sunflower Starfish. I need a way to take that number and make it dynamically tell the HTML file that 13041 means Sunflower Starfish.
    Consider an array look-up of the values and reference name.


    Any help would be appreciated greatly. I'm going to continue to work on this and check back periodically.

    Thanks again,

    --thesprucegoose

  5. #5
    Join Date
    Jan 2008
    Posts
    123
    Thanks.

    Your code works like a charm!

  6. #6
    Join Date
    Jan 2008
    Posts
    123
    My only beef now is importing the .txt file...

    Ultimately, I'd love it if I could make variables out of the stuff I'm getting.

    for example:

    Code:
    photoFile4=pic5.jpg&
    photoCaptionID4=13007&
    photoCaption4=4769616e742050616369666963204f63746f707573&
    photoDepth4=3&
    photoSpecies4=13007-13009-13044-13011&
    taking the information above and have photoDepth4 automatically become a variable with a value of 3.

    Any ideas?

  7. #7
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,843

    Lightbulb Consider this ...

    Assuming the text information always comes in the same order, you could assign to an array for access:
    Code:
    var textStr = 'photoFile4=pic5.jpg&
    photoCaptionID4=13007&
    photoCaption4=4769616e742050616369666963204f63746f707573&
    photoDepth4=3&
    photoSpecies4=13007-13009-13044-13011&';
    
    var parts = textStr.split('&');
    // now parts[0] = 'photoFile4=pic5.jpg'
    // and parts[1] = 'photoCaptionID4=13007'
    // ...
    // and parts[4] = 'photoSpecies4=13007-13009-13044-13011'
    // use the parts array directly or split further as needed for your application, like
    
    var tmp = parts[3].split('=');
    var photoDepth4 = tmp[1];
    // do similar procedure for other variables you need to create.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles