nodejs hexadecimal to string

WBOY
Release: 2023-05-23 17:59:37
Original
1044 people have browsed it

In Node.js, we can use the Buffer object to convert hexadecimal data into strings.

The Buffer object is a built-in object in the Node.js API, used to process binary data, including data represented in hexadecimal format. We can use theBuffer.from()method to convert hexadecimal data to its corresponding Buffer object and output it as a string. For example, assuming we have a hexadecimal string48656c6c6f20576f726c64, we can convert it to a string using the following code:

const hexString = '48656c6c6f20576f726c64'; const buf = Buffer.from(hexString, 'hex'); const str = buf.toString('utf8'); console.log(str);
Copy after login

hereBuffer.from()Method convertshexStringto hexadecimal and stores the result in thebufvariable. Then, we use thebuf.toString()method to decode it into a utf8 formatted string. This will outputHello World.

If our hexadecimal data is separated by spaces, we can use theString.prototype.split()method to split it into individual hexadecimal values and useBuffer.from()method converts it to a string. For example, assuming we have a hexadecimal string48 65 6c 6c 6f 20 57 6f 72 6c 64, we can convert it to a string using the following code:

const hexString = '48 65 6c 6c 6f 20 57 6f 72 6c 64'; const hexArr = hexString.split(' '); const buf = Buffer.from(hexArr, 'hex'); const str = buf.toString('utf8'); console.log(str);
Copy after login

here ThehexString.split(' ')method splitshexStringinto an array containing each hexadecimal value. We then convert it to a string using theBuffer.from()method.

However, it should be noted that if our hexadecimal data contains illegal characters, it cannot be converted to a string correctly. If we try to convert the following string48656c6c6f20576f726c6447to a string, an error occurs because it contains an illegal hexadecimal character47:

const hexString = '48656c6c6f20576f726c6447'; const buf = Buffer.from(hexString, 'hex'); const str = buf.toString('utf8'); console.log(str); // 报错
Copy after login

In this case, we can avoid the program crash by using an error handler when calling theBuffer.from()method. For example, we can use the following code:

const hexString = '48656c6c6f20576f726c6447'; let str; try { const buf = Buffer.from(hexString, 'hex'); str = buf.toString('utf8'); } catch (err) { console.error(err); str = ''; } console.log(str); // 输出空字符串
Copy after login

Thetry..catchblock here catches errors from theBuffer.from()method andstrSet to an empty string to prevent the program from crashing. We can adjust the exception handler appropriately according to the specific situation.

In short, converting hexadecimal data to string is a common task in Node.js, and we can use the related functions of the Buffer object to complete this work.

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

source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!