Act*_*ung 17 javascript arrays data-conversion bitcoin
我有一个表单的字节数组[4,-101,122,-41,-30,23,-28,3,..],我想以6d69f597b217fa333246c2c8
我在函数下面使用的形式转换
function toHexString(bytes) {
return bytes.map(function(byte) {
return (byte & 0xFF).toString(16)
}).join('')
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个相同形式的字符串,但我怀疑它不是一个有效的转换,因为十六进制字符串比预期短一点.我认为翻译应该得到"0a10a6dc".请告诉我,如果我错了或这是一个正确的转换,但也许我没有使用正确的字节数组
字节数组 4,-127,45,126,58,-104,41,-27,-43,27,-35,100,-50,-77,93,-16,96,105,-101,-63,48,-105,49,-67,110,111,26,84,67,-89,-7,-50,10,-12,56,47,-49,-42,-11,-8,-96,-117,-78,97,-105,9,-62,-44,-97,-73,113,96,23,112,-14,-62,103,-104,90,-14,117,78,31,-116,-7
相应的转换 4812d7e3a9829e5d51bdd64ceb35df060699bc1309731bd6e6f1a5443a7f9ceaf4382fcfd6f5f8a08bb261979c2d49fb771601770f2c267985af2754e1f8cf9
Ber*_*rgi 44
您缺少十六进制转换中的填充.你会想要使用
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
Run Code Online (Sandbox Code Playgroud)
这样每个字节就可以转换成两个十六进制数字.你的预期产量是04812d7e3a9829e5d51bdd64ceb35df060699bc1309731bd6e6f1a5443a7f9ce0af4382fcfd6f5f8a08bb2619709c2d49fb771601770f2c267985af2754e1f8cf9
小智 7
map()如果输入的类型如Uint8Array:,则使用将不起作用:的结果map()也Uint8Array不能保存字符串转换的结果。
function toHexString(byteArray) {
var s = '0x';
byteArray.forEach(function(byte) {
s += ('0' + (byte & 0xFF).toString(16)).slice(-2);
});
return s;
}
Run Code Online (Sandbox Code Playgroud)
前面的所有解决方案都有效,但它们都需要创建许多字符串以及对创建的字符串进行串联和切片。我想既然有了类型化数组,就必须有更好的方法来解决这个问题。我最初使用 Node 来完成此操作,然后注释掉使用 Buffer 的行并将它们更改为 TypedArrays,这样它也可以在浏览器中工作。
它的代码更多,但速度明显更快,至少在我放在一起的快速 jsperf(不再工作)中是这样。接受的答案中的字符串操作版本执行 37000 次操作/秒,而下面的代码管理 317000 次操作/秒。创建字符串对象有很多隐藏的开销。
function toHexString (byteArray) {
//const chars = new Buffer(byteArray.length * 2);
const chars = new Uint8Array(byteArray.length * 2);
const alpha = 'a'.charCodeAt(0) - 10;
const digit = '0'.charCodeAt(0);
let p = 0;
for (let i = 0; i < byteArray.length; i++) {
let nibble = byteArray[i] >>> 4;
chars[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
nibble = byteArray[i] & 0xF;
chars[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
}
//return chars.toString('utf8');
return String.fromCharCode.apply(null, chars);
}
Run Code Online (Sandbox Code Playgroud)
使用Array.reduce()更简洁,更高效(参见https://jsperf.com/byte-array-to-hex-string)替代方案:
function toHexString(byteArray) {
return byteArray.reduce((output, elem) =>
(output + ('0' + elem.toString(16)).slice(-2)),
'');
}
Run Code Online (Sandbox Code Playgroud)
(也没有"&0xFF",因为在我看来如果传入一个包含大于255的值的数组,输出应该搞乱,这样用户可以更容易地看到他们的输入是错误的.)
由于这是“js byte to hex”的第一次谷歌命中,我需要一些时间来理解Bergi的功能,我重新编写了该功能并添加了一些注释,使我更容易理解:
function byteToHex(byte) {
// convert the possibly signed byte (-128 to 127) to an unsigned byte (0 to 255).
// if you know, that you only deal with unsigned bytes (Uint8Array), you can omit this line
const unsignedByte = byte & 0xff;
// If the number can be represented with only 4 bits (0-15),
// the hexadecimal representation of this number is only one char (0-9, a-f).
if (unsignedByte < 16) {
return '0' + unsignedByte.toString(16);
} else {
return unsignedByte.toString(16);
}
}
// bytes is an typed array (Int8Array or Uint8Array)
function toHexString(bytes) {
// Since the .map() method is not available for typed arrays,
// we will convert the typed array to an array using Array.from().
return Array.from(bytes)
.map(byte => byteToHex(byte))
.join('');
}
Run Code Online (Sandbox Code Playgroud)
const unsignedByte = byte & 0xff-part 的更多信息,请检查AND 0xFF 的作用是什么?.Array.from并非在所有浏览器中都可用(例如不在 IE11 中),请查看如何将 JavaScript 类型化数组转换为 JavaScript 数组以获取更多信息OP忘记添加0只能用4位显示的数字的前导。
| 归档时间: |
|
| 查看次数: |
33186 次 |
| 最近记录: |