如何将字符串转换为Bytearray

时间:2021-01-09 18:25:38

How can I convert a string in bytearray using JavaScript. Output should be equivalent of the below C# code.

如何使用JavaScript在bytearray中转换字符串。输出应该等于下面的c#代码。

UnicodeEncoding encoding = new UnicodeEncoding();
byte[] bytes = encoding.GetBytes(AnyString);

As UnicodeEncoding is by default of UTF-16 with Little-Endianness.

由于UnicodeEncoding是缺省的UTF-16,并且很少机缘巧合。

Edit: I have a requirement to match the bytearray generated client side with the one generated at server side using the above C# code.

编辑:我需要将bytearray生成的客户端与使用上述c#代码在服务器端生成的客户端进行匹配。

11 个解决方案

#1


11  

In C# running this

在c#中运行

UnicodeEncoding encoding = new UnicodeEncoding();
byte[] bytes = encoding.GetBytes("Hello");

Will create an array with

将创建一个数组

72,0,101,0,108,0,108,0,111,0

如何将字符串转换为Bytearray

For a character which the code is greater than 255 it will look like this

对于代码大于255的字符,应该是这样的

如何将字符串转换为Bytearray

If you want a very similar behavior in JavaScript you can do this (v2 is a bit more robust solution, while the original version will only work for 0x00 ~ 0xff)

如果希望JavaScript中有非常类似的行为,可以这样做(v2是更健壮的解决方案,而原始版本只适用于0x00 ~ 0xff)

var str = "Hello竜";
var bytes = []; // char codes
var bytesv2 = []; // char codes

for (var i = 0; i < str.length; ++i) {
  var code = str.charCodeAt(i);
  
  bytes = bytes.concat([code]);
  
  bytesv2 = bytesv2.concat([code & 0xff, code / 256 >>> 0]);
}

// 72, 101, 108, 108, 111, 31452
console.log('bytes', bytes.join(', '));

// 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 220, 122
console.log('bytesv2', bytesv2.join(', '));

#2


15  

If you are looking for a solution that works in node.js, you can use this:

如果您正在寻找在node中工作的解决方案。js,你可以用这个:

var myBuffer = [];
var str = 'Stack Overflow';
var buffer = new Buffer(str, 'utf16le');
for (var i = 0; i < buffer.length; i++) {
    myBuffer.push(buffer[i]);
}

console.log(myBuffer);

#3


12  

I suppose C# and Java produce equal byte arrays. If you have non-ASCII characters, it's not enough to add an additional 0. My example contains a few special characters:

我认为c#和Java生成了相等的字节数组。如果您有非ascii字符,仅添加一个额外的0是不够的。我的例子包含一些特殊的字符:

var str = "Hell ö € Ω ????";
var bytes = [];
var charCode;

for (var i = 0; i < str.length; ++i)
{
    charCode = str.charCodeAt(i);
    bytes.push((charCode & 0xFF00) >> 8);
    bytes.push(charCode & 0xFF);
}

alert(bytes.join(' '));
// 0 72 0 101 0 108 0 108 0 32 0 246 0 32 32 172 0 32 3 169 0 32 216 52 221 30

I don't know if C# places BOM (Byte Order Marks), but if using UTF-16, Java String.getBytes adds following bytes: 254 255.

我不知道c#是否放置BOM(字节顺序标记),但是如果使用UTF-16, Java字符串。getBytes添加了以下字节:254255。

String s = "Hell ö € Ω ";
// now add a character outside the BMP (Basic Multilingual Plane)
// we take the violin-symbol (U+1D11E) MUSICAL SYMBOL G CLEF
s += new String(Character.toChars(0x1D11E));
// surrogate codepoints are: d834, dd1e, so one could also write "\ud834\udd1e"

byte[] bytes = s.getBytes("UTF-16");
for (byte aByte : bytes) {
    System.out.print((0xFF & aByte) + " ");
}
// 254 255 0 72 0 101 0 108 0 108 0 32 0 246 0 32 32 172 0 32 3 169 0 32 216 52 221 30

Edit:

编辑:

Added a special character (U+1D11E) MUSICAL SYMBOL G CLEF (outside BPM, so taking not only 2 bytes in UTF-16, but 4.

添加了一个特殊的字符(U+1D11E)音乐符号G CLEF (BPM外,因此在UTF-16中不仅取2个字节,而且取4个字节。

Current JavaScript versions use "UCS-2" internally, so this symbol takes the space of 2 normal characters.

当前的JavaScript版本在内部使用“UCS-2”,因此这个符号占用两个普通字符的空间。

I'm not sure but when using charCodeAt it seems we get exactly the surrogate codepoints also used in UTF-16, so non-BPM characters are handled correctly.

我不确定,但是在使用charCodeAt时,我们似乎得到了UTF-16中也使用的代理代码点,因此正确地处理非bpm字符。

This problem is absolutely non-trivial. It might depend on the used JavaScript versions and engines. So if you want reliable solutions, you should have a look at:

这个问题绝对不平凡。它可能依赖于使用的JavaScript版本和引擎。因此,如果你想要可靠的解决方案,你应该看看:

#4


7  

Inspired by @hgoebl's answer. His code is for UTF-16 and I needed something for US-ASCII. So here's a more complete answer covering US-ASCII, UTF-16, and UTF-32.

灵感来自于@hgoebl的答案。他的代码是UTF-16,我需要一些美国ascii码。这里有一个更完整的答案,包括US-ASCII、UTF-16和UTF-32。

function stringToAsciiByteArray(str)
{
    var bytes = [];
   for (var i = 0; i < str.length; ++i)
   {
       var charCode = str.charCodeAt(i);
      if (charCode > 0xFF)  // char > 1 byte since charCodeAt returns the UTF-16 value
      {
          throw new Error('Character ' + String.fromCharCode(charCode) + ' can\'t be represented by a US-ASCII byte.');
      }
       bytes.push(charCode);
   }
    return bytes;
}
function stringToUtf16ByteArray(str)
{
    var bytes = [];
    //currently the function returns without BOM. Uncomment the next line to change that.
    //bytes.push(254, 255);  //Big Endian Byte Order Marks
   for (var i = 0; i < str.length; ++i)
   {
       var charCode = str.charCodeAt(i);
       //char > 2 bytes is impossible since charCodeAt can only return 2 bytes
       bytes.push((charCode & 0xFF00) >>> 8);  //high byte (might be 0)
       bytes.push(charCode & 0xFF);  //low byte
   }
    return bytes;
}
function stringToUtf32ByteArray(str)
{
    var bytes = [];
    //currently the function returns without BOM. Uncomment the next line to change that.
    //bytes.push(0, 0, 254, 255);  //Big Endian Byte Order Marks
   for (var i = 0; i < str.length; i+=2)
   {
       var charPoint = str.codePointAt(i);
       //char > 4 bytes is impossible since codePointAt can only return 4 bytes
       bytes.push((charPoint & 0xFF000000) >>> 24);
       bytes.push((charPoint & 0xFF0000) >>> 16);
       bytes.push((charPoint & 0xFF00) >>> 8);
       bytes.push(charPoint & 0xFF);
   }
    return bytes;
}

UTF-8 is variable length and isn't included because I would have to write the encoding myself. UTF-8 and UTF-16 are variable length. UTF-8, UTF-16, and UTF-32 have a minimum number of bits as their name indicates. If a UTF-32 character has a code point of 65 then that means there are 3 leading 0s. But the same code for UTF-16 has only 1 leading 0. US-ASCII on the other hand is fixed width 8-bits which means it can be directly translated to bytes.

UTF-8是可变长度的,不包括在内,因为我必须自己编写编码。UTF-8和UTF-16是可变长度。UTF-8、UTF-16和UTF-32具有它们的名称所指示的最小位数。如果一个UTF-32字符的编码点是65,那就意味着有3个领先的0。但是同样的UTF-16代码只有1个前导0。另一方面,US-ASCII是固定宽度的8位,这意味着它可以直接转换为字节。

String.prototype.charCodeAt returns a maximum number of 2 bytes and matches UTF-16 exactly. However for UTF-32 String.prototype.codePointAt is needed which is part of the ECMAScript 6 (Harmony) proposal. Because charCodeAt returns 2 bytes which is more possible characters than US-ASCII can represent, the function stringToAsciiByteArray will throw in such cases instead of splitting the character in half and taking either or both bytes.

String.prototype。codecharat返回最多2个字节,并完全匹配UTF-16。然而utf - 32 String.prototype。需要codePointAt,它是ECMAScript 6 (Harmony)建议的一部分。因为codecharat返回2个字节,这比US-ASCII所代表的字符更可能,所以stringToAsciiByteArray函数将在这种情况下抛出字符,而不是将字符分成两半,取一个或两个字节。

Note that this answer is non-trivial because character encoding is non-trivial. What kind of byte array you want depends on what character encoding you want those bytes to represent.

注意,这个答案是非平凡的,因为字符编码是非平凡的。你想要的字节数组取决于你想要那些字节的字符编码。

javascript has the option of internally using either UTF-16 or UCS-2 but since it has methods that act like it is UTF-16 I don't see why any browser would use UCS-2. Also see: https://mathiasbynens.be/notes/javascript-encoding

javascript可以在内部使用UTF-16或UCS-2,但是由于它的方法像UTF-16一样,我不明白为什么任何浏览器都使用UCS-2。还看到:https://mathiasbynens.be/notes/javascript-encoding

Yes I know the question is 4 years old but I needed this answer for myself.

是的,我知道这个问题已经有4年了,但我自己需要这个答案。

#5


1  

Here is the same function that @BrunoLM posted converted to a String prototype function:

下面是@BrunoLM发布的与字符串原型函数相同的函数:

String.prototype.getBytes = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
    bytes.push(this.charCodeAt(i));
  }
  return bytes;
};

If you define the function as such, then you can call the .getBytes() method on any string:

如果将函数定义为这样,那么可以在任何字符串上调用.getBytes()方法:

var str = "Hello World!";
var bytes = str.getBytes();

#6


1  

Since I cannot comment on the answer, I'd build on Jin Izzraeel's answer

既然我不能对这个答案发表评论,我就以金·伊兹瑞尔的回答为基础

var myBuffer = [];
var str = 'Stack Overflow';
var buffer = new Buffer(str, 'utf16le');
for (var i = 0; i < buffer.length; i++) {
    myBuffer.push(buffer[i]);
}

console.log(myBuffer);

by saying that you could use this if you want to use a Node.js buffer in your browser.

如果你想要使用一个节点,你可以使用它。浏览器中的js缓冲区。

https://github.com/feross/buffer

https://github.com/feross/buffer

Therefore, Tom Stickel's objection is not valid, and the answer is indeed a valid answer.

因此,Tom Stickel的反对是无效的,答案确实是有效的。

#7


1  

String.prototype.encodeHex = function () {
    return this.split('').map(e => e.charCodeAt())
};

String.prototype.decodeHex = function () {    
    return this.map(e => String.fromCharCode(e)).join('')
};

#8


0  

The best solution I've come up with at on the spot (though most likely crude) would be:

我在现场提出的最佳解决方案(尽管很可能是原油)是:

String.prototype.getBytes = function() {
    var bytes = [];
    for (var i = 0; i < this.length; i++) {
        var charCode = this.charCodeAt(i);
        var cLen = Math.ceil(Math.log(charCode)/Math.log(256));
        for (var j = 0; j < cLen; j++) {
            bytes.push((charCode << (j*8)) & 0xFF);
        }
    }
    return bytes;
}

Though I notice this question has been here for over a year.

虽然我注意到这个问题已经存在一年多了。

#9


0  

I know the question is almost 4 years old, but this is what worked smoothly with me:

我知道这个问题已经问了快4年了,但这正是我一直在思考的问题:

String.prototype.encodeHex = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
    bytes.push(this.charCodeAt(i));
  }
  return bytes;
};

Array.prototype.decodeHex = function () {    
  var str = [];
  var hex = this.toString().split(',');
  for (var i = 0; i < hex.length; i++) {
    str.push(String.fromCharCode(hex[i]));
  }
  return str.toString().replace(/,/g, "");
};

var str = "Hello World!";
var bytes = str.encodeHex();

alert('The Hexa Code is: '+bytes+' The original string is:  '+bytes.decodeHex());

or, if you want to work with strings only, and no Array, you can use:

或者,如果您想只处理字符串,而不处理数组,您可以使用:

String.prototype.encodeHex = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
    bytes.push(this.charCodeAt(i));
  }
  return bytes.toString();
};

String.prototype.decodeHex = function () {    
  var str = [];
  var hex = this.split(',');
  for (var i = 0; i < hex.length; i++) {
    str.push(String.fromCharCode(hex[i]));
  }
  return str.toString().replace(/,/g, "");
};

var str = "Hello World!";
var bytes = str.encodeHex();

alert('The Hexa Code is: '+bytes+' The original string is:  '+bytes.decodeHex());

#10


0  

The easiest way in 2018 should be TextEncoder but the returned element is not byte array, it is Uint8Array. (And not all browsers support it)

2018年最简单的方法应该是TextEncoder,但是返回的元素不是字节数组,而是Uint8Array。(并不是所有的浏览器都支持它)

let utf8Decode = new TextDecoder('utf-8');
utf8Encode.encode("eee")
> Uint8Array [ 101, 101, 101 ]

#11


-1  

You don't need underscore, just use built-in map:

你不需要下划线,只需使用内建的地图:

var string = 'Hello World!';

document.write(string.split('').map(function(c) { return c.charCodeAt(); }));

#1


11  

In C# running this

在c#中运行

UnicodeEncoding encoding = new UnicodeEncoding();
byte[] bytes = encoding.GetBytes("Hello");

Will create an array with

将创建一个数组

72,0,101,0,108,0,108,0,111,0

如何将字符串转换为Bytearray

For a character which the code is greater than 255 it will look like this

对于代码大于255的字符,应该是这样的

如何将字符串转换为Bytearray

If you want a very similar behavior in JavaScript you can do this (v2 is a bit more robust solution, while the original version will only work for 0x00 ~ 0xff)

如果希望JavaScript中有非常类似的行为,可以这样做(v2是更健壮的解决方案,而原始版本只适用于0x00 ~ 0xff)

var str = "Hello竜";
var bytes = []; // char codes
var bytesv2 = []; // char codes

for (var i = 0; i < str.length; ++i) {
  var code = str.charCodeAt(i);
  
  bytes = bytes.concat([code]);
  
  bytesv2 = bytesv2.concat([code & 0xff, code / 256 >>> 0]);
}

// 72, 101, 108, 108, 111, 31452
console.log('bytes', bytes.join(', '));

// 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 220, 122
console.log('bytesv2', bytesv2.join(', '));

#2


15  

If you are looking for a solution that works in node.js, you can use this:

如果您正在寻找在node中工作的解决方案。js,你可以用这个:

var myBuffer = [];
var str = 'Stack Overflow';
var buffer = new Buffer(str, 'utf16le');
for (var i = 0; i < buffer.length; i++) {
    myBuffer.push(buffer[i]);
}

console.log(myBuffer);

#3


12  

I suppose C# and Java produce equal byte arrays. If you have non-ASCII characters, it's not enough to add an additional 0. My example contains a few special characters:

我认为c#和Java生成了相等的字节数组。如果您有非ascii字符,仅添加一个额外的0是不够的。我的例子包含一些特殊的字符:

var str = "Hell ö € Ω ????";
var bytes = [];
var charCode;

for (var i = 0; i < str.length; ++i)
{
    charCode = str.charCodeAt(i);
    bytes.push((charCode & 0xFF00) >> 8);
    bytes.push(charCode & 0xFF);
}

alert(bytes.join(' '));
// 0 72 0 101 0 108 0 108 0 32 0 246 0 32 32 172 0 32 3 169 0 32 216 52 221 30

I don't know if C# places BOM (Byte Order Marks), but if using UTF-16, Java String.getBytes adds following bytes: 254 255.

我不知道c#是否放置BOM(字节顺序标记),但是如果使用UTF-16, Java字符串。getBytes添加了以下字节:254255。

String s = "Hell ö € Ω ";
// now add a character outside the BMP (Basic Multilingual Plane)
// we take the violin-symbol (U+1D11E) MUSICAL SYMBOL G CLEF
s += new String(Character.toChars(0x1D11E));
// surrogate codepoints are: d834, dd1e, so one could also write "\ud834\udd1e"

byte[] bytes = s.getBytes("UTF-16");
for (byte aByte : bytes) {
    System.out.print((0xFF & aByte) + " ");
}
// 254 255 0 72 0 101 0 108 0 108 0 32 0 246 0 32 32 172 0 32 3 169 0 32 216 52 221 30

Edit:

编辑:

Added a special character (U+1D11E) MUSICAL SYMBOL G CLEF (outside BPM, so taking not only 2 bytes in UTF-16, but 4.

添加了一个特殊的字符(U+1D11E)音乐符号G CLEF (BPM外,因此在UTF-16中不仅取2个字节,而且取4个字节。

Current JavaScript versions use "UCS-2" internally, so this symbol takes the space of 2 normal characters.

当前的JavaScript版本在内部使用“UCS-2”,因此这个符号占用两个普通字符的空间。

I'm not sure but when using charCodeAt it seems we get exactly the surrogate codepoints also used in UTF-16, so non-BPM characters are handled correctly.

我不确定,但是在使用charCodeAt时,我们似乎得到了UTF-16中也使用的代理代码点,因此正确地处理非bpm字符。

This problem is absolutely non-trivial. It might depend on the used JavaScript versions and engines. So if you want reliable solutions, you should have a look at:

这个问题绝对不平凡。它可能依赖于使用的JavaScript版本和引擎。因此,如果你想要可靠的解决方案,你应该看看:

#4


7  

Inspired by @hgoebl's answer. His code is for UTF-16 and I needed something for US-ASCII. So here's a more complete answer covering US-ASCII, UTF-16, and UTF-32.

灵感来自于@hgoebl的答案。他的代码是UTF-16,我需要一些美国ascii码。这里有一个更完整的答案,包括US-ASCII、UTF-16和UTF-32。

function stringToAsciiByteArray(str)
{
    var bytes = [];
   for (var i = 0; i < str.length; ++i)
   {
       var charCode = str.charCodeAt(i);
      if (charCode > 0xFF)  // char > 1 byte since charCodeAt returns the UTF-16 value
      {
          throw new Error('Character ' + String.fromCharCode(charCode) + ' can\'t be represented by a US-ASCII byte.');
      }
       bytes.push(charCode);
   }
    return bytes;
}
function stringToUtf16ByteArray(str)
{
    var bytes = [];
    //currently the function returns without BOM. Uncomment the next line to change that.
    //bytes.push(254, 255);  //Big Endian Byte Order Marks
   for (var i = 0; i < str.length; ++i)
   {
       var charCode = str.charCodeAt(i);
       //char > 2 bytes is impossible since charCodeAt can only return 2 bytes
       bytes.push((charCode & 0xFF00) >>> 8);  //high byte (might be 0)
       bytes.push(charCode & 0xFF);  //low byte
   }
    return bytes;
}
function stringToUtf32ByteArray(str)
{
    var bytes = [];
    //currently the function returns without BOM. Uncomment the next line to change that.
    //bytes.push(0, 0, 254, 255);  //Big Endian Byte Order Marks
   for (var i = 0; i < str.length; i+=2)
   {
       var charPoint = str.codePointAt(i);
       //char > 4 bytes is impossible since codePointAt can only return 4 bytes
       bytes.push((charPoint & 0xFF000000) >>> 24);
       bytes.push((charPoint & 0xFF0000) >>> 16);
       bytes.push((charPoint & 0xFF00) >>> 8);
       bytes.push(charPoint & 0xFF);
   }
    return bytes;
}

UTF-8 is variable length and isn't included because I would have to write the encoding myself. UTF-8 and UTF-16 are variable length. UTF-8, UTF-16, and UTF-32 have a minimum number of bits as their name indicates. If a UTF-32 character has a code point of 65 then that means there are 3 leading 0s. But the same code for UTF-16 has only 1 leading 0. US-ASCII on the other hand is fixed width 8-bits which means it can be directly translated to bytes.

UTF-8是可变长度的,不包括在内,因为我必须自己编写编码。UTF-8和UTF-16是可变长度。UTF-8、UTF-16和UTF-32具有它们的名称所指示的最小位数。如果一个UTF-32字符的编码点是65,那就意味着有3个领先的0。但是同样的UTF-16代码只有1个前导0。另一方面,US-ASCII是固定宽度的8位,这意味着它可以直接转换为字节。

String.prototype.charCodeAt returns a maximum number of 2 bytes and matches UTF-16 exactly. However for UTF-32 String.prototype.codePointAt is needed which is part of the ECMAScript 6 (Harmony) proposal. Because charCodeAt returns 2 bytes which is more possible characters than US-ASCII can represent, the function stringToAsciiByteArray will throw in such cases instead of splitting the character in half and taking either or both bytes.

String.prototype。codecharat返回最多2个字节,并完全匹配UTF-16。然而utf - 32 String.prototype。需要codePointAt,它是ECMAScript 6 (Harmony)建议的一部分。因为codecharat返回2个字节,这比US-ASCII所代表的字符更可能,所以stringToAsciiByteArray函数将在这种情况下抛出字符,而不是将字符分成两半,取一个或两个字节。

Note that this answer is non-trivial because character encoding is non-trivial. What kind of byte array you want depends on what character encoding you want those bytes to represent.

注意,这个答案是非平凡的,因为字符编码是非平凡的。你想要的字节数组取决于你想要那些字节的字符编码。

javascript has the option of internally using either UTF-16 or UCS-2 but since it has methods that act like it is UTF-16 I don't see why any browser would use UCS-2. Also see: https://mathiasbynens.be/notes/javascript-encoding

javascript可以在内部使用UTF-16或UCS-2,但是由于它的方法像UTF-16一样,我不明白为什么任何浏览器都使用UCS-2。还看到:https://mathiasbynens.be/notes/javascript-encoding

Yes I know the question is 4 years old but I needed this answer for myself.

是的,我知道这个问题已经有4年了,但我自己需要这个答案。

#5


1  

Here is the same function that @BrunoLM posted converted to a String prototype function:

下面是@BrunoLM发布的与字符串原型函数相同的函数:

String.prototype.getBytes = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
    bytes.push(this.charCodeAt(i));
  }
  return bytes;
};

If you define the function as such, then you can call the .getBytes() method on any string:

如果将函数定义为这样,那么可以在任何字符串上调用.getBytes()方法:

var str = "Hello World!";
var bytes = str.getBytes();

#6


1  

Since I cannot comment on the answer, I'd build on Jin Izzraeel's answer

既然我不能对这个答案发表评论,我就以金·伊兹瑞尔的回答为基础

var myBuffer = [];
var str = 'Stack Overflow';
var buffer = new Buffer(str, 'utf16le');
for (var i = 0; i < buffer.length; i++) {
    myBuffer.push(buffer[i]);
}

console.log(myBuffer);

by saying that you could use this if you want to use a Node.js buffer in your browser.

如果你想要使用一个节点,你可以使用它。浏览器中的js缓冲区。

https://github.com/feross/buffer

https://github.com/feross/buffer

Therefore, Tom Stickel's objection is not valid, and the answer is indeed a valid answer.

因此,Tom Stickel的反对是无效的,答案确实是有效的。

#7


1  

String.prototype.encodeHex = function () {
    return this.split('').map(e => e.charCodeAt())
};

String.prototype.decodeHex = function () {    
    return this.map(e => String.fromCharCode(e)).join('')
};

#8


0  

The best solution I've come up with at on the spot (though most likely crude) would be:

我在现场提出的最佳解决方案(尽管很可能是原油)是:

String.prototype.getBytes = function() {
    var bytes = [];
    for (var i = 0; i < this.length; i++) {
        var charCode = this.charCodeAt(i);
        var cLen = Math.ceil(Math.log(charCode)/Math.log(256));
        for (var j = 0; j < cLen; j++) {
            bytes.push((charCode << (j*8)) & 0xFF);
        }
    }
    return bytes;
}

Though I notice this question has been here for over a year.

虽然我注意到这个问题已经存在一年多了。

#9


0  

I know the question is almost 4 years old, but this is what worked smoothly with me:

我知道这个问题已经问了快4年了,但这正是我一直在思考的问题:

String.prototype.encodeHex = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
    bytes.push(this.charCodeAt(i));
  }
  return bytes;
};

Array.prototype.decodeHex = function () {    
  var str = [];
  var hex = this.toString().split(',');
  for (var i = 0; i < hex.length; i++) {
    str.push(String.fromCharCode(hex[i]));
  }
  return str.toString().replace(/,/g, "");
};

var str = "Hello World!";
var bytes = str.encodeHex();

alert('The Hexa Code is: '+bytes+' The original string is:  '+bytes.decodeHex());

or, if you want to work with strings only, and no Array, you can use:

或者,如果您想只处理字符串,而不处理数组,您可以使用:

String.prototype.encodeHex = function () {
  var bytes = [];
  for (var i = 0; i < this.length; ++i) {
    bytes.push(this.charCodeAt(i));
  }
  return bytes.toString();
};

String.prototype.decodeHex = function () {    
  var str = [];
  var hex = this.split(',');
  for (var i = 0; i < hex.length; i++) {
    str.push(String.fromCharCode(hex[i]));
  }
  return str.toString().replace(/,/g, "");
};

var str = "Hello World!";
var bytes = str.encodeHex();

alert('The Hexa Code is: '+bytes+' The original string is:  '+bytes.decodeHex());

#10


0  

The easiest way in 2018 should be TextEncoder but the returned element is not byte array, it is Uint8Array. (And not all browsers support it)

2018年最简单的方法应该是TextEncoder,但是返回的元素不是字节数组,而是Uint8Array。(并不是所有的浏览器都支持它)

let utf8Decode = new TextDecoder('utf-8');
utf8Encode.encode("eee")
> Uint8Array [ 101, 101, 101 ]

#11


-1  

You don't need underscore, just use built-in map:

你不需要下划线,只需使用内建的地图:

var string = 'Hello World!';

document.write(string.split('').map(function(c) { return c.charCodeAt(); }));