Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By rnd me
  • 1 Post By Philip M

Thread: Javascript - Byte Array to String (UTF8)

  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript - Byte Array to String (UTF8)

    Hi All,

    Please help me.

    I am getting UTF-8 bytes as below

    Code:
    3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E
    I want java script sample code which will convert above bytes to corresponding text as shown below

    Code:
    <ROOT>
       <STRING>TEST ONLY STRING</STRING>
    </ROOT>
    Your valuable help is much appreciated.


    Thanks
    Raj

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by Raj4Smile View Post
    I want java script sample code which will convert above bytes to corresponding text as shown below

    <ROOT> <STRING>TEST ONLY STRING</STRING></ROOT>
    Code:
    <script type = "text/javascript">
    
    function hex2a(hexx) {
    var hex = hexx.toString(); //force conversion
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
    str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
    }
    
    alert(hex2a("3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E")); 
    
    </script>
    The reverse function:-

    Code:
    <script type = "text/javascript">
    
    function a2hex(str) {
    var arr = [];
    for (var i = 0, l = str.length; i < l; i ++) {
    var hex = Number(str.charCodeAt(i)).toString(16);
    arr.push(hex);
    }
    return arr.join('');
    }
    alert(a2hex("<ROOT><STRING>TEST ONLY STRING</STRING></ROOT>"));
    
    </script>
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 04-01-2014 at 06:53 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    another way:
    Code:
    "3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E"
      .match(/\w\w/g).map(function(a){return String.fromCharCode(parseInt(a, 16))}).join("")
    Raj4Smile likes this.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,309
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    And another way:-

    Code:
    <script type = "text/javascript">
    
    var str = "3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E";
    var str2 = "";
    for (var i=0; i<str.length; i=i+2) {str2=str2+'%'+str.substr(i,2);}
    alert (unescape(str2));
    document.write(unescape(str2));  // note that HTML tags are not output
    
    </script>
    Raj4Smile likes this.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    another way:
    Code:
    "3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E"
      .match(/\w\w/g).map(function(a){return String.fromCharCode(parseInt(a, 16))}).join("")


    Thanks all. Its done with your great help. You are awesome

  • #6
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks all. This problem is resolved now. Philip, rnd -- lots of thanks to you.


  •  

    Posting Permissions

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