简体   繁体   中英

How to trim hex and convert it to string

Let's say I have a line of hex

4f6c69766572204d6973202020202020

When I use my Javascript function to convert it into string and return this:

Oliver Mis <-- there are spaces at the end, because 202020202020 is spaces.

How can I remove the extra spaces at the end of the string. Because space between Oliver' 'Mis , I don't want to remove it. I'm trying to do something like if it meets 2 spaces, then only take up to 4f6c69766572204d6973 which is Oliver Mis.

This is my convert hex into string function:

function hextostring(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;
}

Use str.Trim() :

function hextostring(hexx) {
    var hex = hexx.toString();
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str.Trim();
}

Removes all leading and trailing white-space characters from the current String object.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM