简体   繁体   English

如何修剪十六进制并将其转换为字符串

[英]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: 当我使用Javascript函数将其转换为字符串并返回以下代码时:

Oliver Mis <-- there are spaces at the end, because 202020202020 is spaces. Oliver Mis <-末尾有空格,因为202020202020是空格。

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. 由于间距Oliver' 'Mis ,我不想将其删除。 I'm trying to do something like if it meets 2 spaces, then only take up to 4f6c69766572204d6973 which is Oliver Mis. 我正在尝试做一些事情,如果它遇到2个空格,则只占用4f6c69766572204d6973 ,即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() : 使用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. 从当前String对象中删除所有前导和尾随空格字符。

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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