Home  >  Article  >  Web Front-end  >  How to convert nodejs byte to each other

How to convert nodejs byte to each other

PHPz
PHPzOriginal
2023-04-26 09:07:331061browse

Node.js is a popular development platform that is powered by the JavaScript language and can be used to build large, highly scalable web applications. In Node.js, processing binary data is very common. Therefore, using Node.js to perform byte conversion is also a very important topic.

Byte to HexString

In Node.js, converting a byte array to a hexadecimal string is very simple. We can use the Buffer class to process it. Here is a sample code that converts a byte array to a hexadecimal string:

const buff = Buffer.from([0x12, 0x34, 0x56, 0x78]);
const hexString = buff.toString('hex');
console.log(`hexString: ${hexString}`);
// hexString: 12345678

The Buffer.from method accepts an array as a parameter and returns a Buffer instance containing these bytes. We can pass it an empty Buffer instance of a specified length and then use the fill method to fill it. If necessary, we can also use the slice() method to intercept the part we need.

The toString() method accepts an optional encoding parameter, here we use hex encoding. This example code outputs a hexadecimal string, and we can see that its output is 12345678.

HexString to Byte

In Node.js, converting a hexadecimal string to a byte array is also very simple. We only need to convert this hexadecimal string Just import it into the Buffer class. The following is a sample code that converts a hexadecimal string into a byte array:

const hexString = 'ab12fde9';
const byteArr = Buffer.from(hexString, 'hex');
console.log(`byteArr: ${byteArr}`);
// byteArr: <Buffer ab 12 fd e9>

The first parameter of the Buffer.from method is a string type, indicating where we want to come from. Here we specify it as a hexadecimal string rather than an array.

The second parameter is optional, it indicates what encoding to use to decode the first parameter. Here we set it to hex.

Convert a byte array to a decimal number

In Node.js, converting a byte array to a decimal number is also very simple. We can use the ArrayBuffer class and DataView class to achieve this function. The following is a sample code that converts a byte array to a decimal number:

const byteArr = new Uint8Array([0x12, 0x34, 0x56, 0x78]);
const buffer = new ArrayBuffer(4);
const dataView = new DataView(buffer);
byteArr.forEach((item, index) => {
  dataView.setUint8(index, item);
});
const decNum = dataView.getUint32(0, false);
console.log(`decNum: ${decNum}`);
// decNum: 305419896

This sample code uses the ArrayBuffer class to create a buffer of size 4. Then we used the DataView class to read and write the data in this buffer.

First, we create an object of type Uint8Array called byteArr and set it to the byte array we want to convert.

Next, we created Buffer and DataView instance objects named buffer and dataView. Then, we use the DataView's setUint8() method to write each element in the byteArr to the buffer.

Finally, we use the getUint32() method of DataView to read the data in the buffer and return a decimal number. The output of this sample code is 305419896.

Convert a decimal number to a byte array

Similarly, in Node.js, converting a decimal number to a byte array is very simple. We just need to use the setUintX() method of the DataView class. The following is a sample code that converts a decimal number to a byte array:

const decNum = 305419896;
const buffer = new ArrayBuffer(4);
const dataView = new DataView(buffer);
dataView.setUint32(0, decNum, false);
const byteArr = new Uint8Array(buffer);
console.log(`byteArr: ${byteArr}`);
// byteArr: Uint8Array [ 18, 52, 86, 120 ]

This sample code uses the setUint32 method of DataView to write a decimal number to a new ArrayBuffer instance. Next we convert this ArrayBuffer instance into a byte array by passing it into a Uint8Array.

The output result of this sample code is a byte array with values ​​[18, 52, 86, 120].

Summary

In Node.js, operations such as converting byte to HexString, converting HexString to byte, converting decimal numbers to byte arrays, and converting byte arrays to decimal numbers are very common. We can use the Buffer class and DataView class provided by Node.js to implement these operations. Hope this article helps you!

The above is the detailed content of How to convert nodejs byte to each other. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn