如何使用JavaScript将字符串转换为数值(应该是可逆的)? [英] How can I convert a string to numeric values (should be reversible) using JavaScript?

查看:82
本文介绍了如何使用JavaScript将字符串转换为数值(应该是可逆的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将JavaScript字符串转换为唯一的数值,以便将数值转换回字符串?数值的长度无关紧要。

How can I convert a JavaScript string into a unique numeric value such that you can convert the numeric value back to the string? The length of the numeric value doesn't matter.

如果这不可能,那么如何将字符串转换为数字值(可逆或不可逆)? / p>

If that's not possible, then how can I convert a string to a numeric value (reversible or not reversible)?

推荐答案

字符串和数字:



字符串可以描述为带编码的字节系列。这会导致显示字符而不是字节值。

Strings and Numbers:

A string can be described as a series of bytes with encoding. That results in characters being shown instead of the byte values.

获取某个字符的字符代码,您将调用 charCodeAt 字符串上的方法。

to get the character code for a certain character you would call the charCodeAt method on a string.

"a".charCodeAt(0); // => 97
"A".charCodeAt(0); // => 65

因此您可以看到每个字母及其变体都有一个数值。现在下一个问题是如何存储这些数字。例如,你不能只是将它们加在一起,因为:

so as you can see there is a numeric value for each letter and it's variants. Now the next problem is how to store these numbers. For example you cannot just add them together because:

97 + 65 = 162;
String.fromCharCode(162); // => ¢

最好的解决方案可能是将它们存储在一个数组中,以便以后轻松重新组装。 / p>

The best solution is probably to store them in an array so they can be easy reassembled later.

var StringTools = {
  stringToNumbersArray: function(data){
    return data.split('').map(function(c){return c.charCodeAt(0);});
  },
  numbersArrayToString: function(arr){
    return arr.map(function(n){return String.fromCharCode(n)}).join('');
  }
}

StringTools.stringToNumbersArray("Hello");
// => [72, 101, 108, 108, 111]
StringTools.numbersArrayToString([72, 101, 108, 108, 111])
// => "Hello"



编码字符串值:



现在,您完全有可能想要支持不是数组的东西(反映整个单词的单个数字串)。要做到这一点,你需要尽可能多的数字来存储集合中最大的值,即3。所以我们可以将这些数字一起推送到一个数字,只要我们知道这个长度,我们可以稍后将它们分开。

Encoding String Values:

Now it's entirely likely that you are wanting to support something that is not an array (a single string of numbers that reflects the entire word). To do this you need as many digits as would be required to store the largest value in the set, which is 3. So we can push these numbers together into a single number as long as we know this length and we can pull them apart later.

[72, 101, 108, 108, 111].reduce(function(memo, n){
  return memo + ("000" + n).slice(-3);
}, "");
// => "072101108108111"

但是你可以看到这不是一个真实的数字,因为开头的0是一个重要的数字。更常见的是,十六进制用于存储这些类型的值。考虑使用 toString 方法可以将每个数字转换为十六进制,其中参数是基数(数字的基数,十六进制为16)。

but as you can see this isn't a real number since the 0 at the beginning is a significant digit. More commonly hexadecimal is used to store these types of values. Consider that each number can be converted to hexadecimal using the toString method where the argument is the radix (base of numbers, 16 for hex).

[72, 101, 108, 108, 111].reduce(function(memo, n){
  return memo + ("00" + n.toString(16)).slice(-2);
}, "");
// => "48656c6c6f"

你可以看到这节省了几个字符,因为我们知道4位可以是存储在单个基数16(1111 === 15)中,因此您只需使用2个基数16(16 * 16 = 256)即可获得完整的存储字节。

as you can see this saved us a few characters because we know that 4 bits can be stored in a single base 16 number (1111 === 15) and so you can get full byte of storage using just 2 base 16 numbers (16 * 16 = 256).

要将十六进制代码反转为原始字符串,您只需要将字符串拆分为2对并将它们转换回基数10,然后使用 String.fromCharCode()获得每个角色的结果。

To reverse the hexadecimal code out to the original string you only need to split the string into pairs of 2 and convert them back to base 10, then use String.fromCharCode() on the result to get each character.

"48656c6c6f".split("").reduce(function(result, c, idx, list){
  if (idx % 2 != 0) {
    result += String.fromCharCode(parseInt(list[idx-1] + c, 16));
  }
  return result;
}, "");

这篇关于如何使用JavaScript将字符串转换为数值(应该是可逆的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆