最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Javascript - How to convert buffer to a string? - Stack Overflow

programmeradmin2浏览0评论

This is example of converting String to a Buffer and back to String:

let bufferOne = Buffer.from('This is a buffer example.');
console.log(bufferOne);

// Output: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>

let json = JSON.stringify(bufferOne);
let bufferOriginal = Buffer.from(JSON.parse(json).data);
console.log(bufferOriginal.toString('utf8'));
// Output: This is a buffer example.

Now imagine someone just give you only this string as a starting point:
<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>
- how would you convert it to regular value of this 'buffer' string?

I tried with:

   let buffer = '<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>'
    json = JSON.stringify(buffer);
    console.log(json);

Gives output:

"<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>"

This is example of converting String to a Buffer and back to String:

let bufferOne = Buffer.from('This is a buffer example.');
console.log(bufferOne);

// Output: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>

let json = JSON.stringify(bufferOne);
let bufferOriginal = Buffer.from(JSON.parse(json).data);
console.log(bufferOriginal.toString('utf8'));
// Output: This is a buffer example.

Now imagine someone just give you only this string as a starting point:
<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>
- how would you convert it to regular value of this 'buffer' string?

I tried with:

   let buffer = '<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>'
    json = JSON.stringify(buffer);
    console.log(json);

Gives output:

"<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>"
Share Improve this question edited Mar 7, 2019 at 12:04 Joe asked Mar 7, 2019 at 10:47 JoeJoe 13.1k39 gold badges123 silver badges200 bronze badges 5
  • What is your expected output? – kockburn Commented Mar 7, 2019 at 10:48
  • you mean convert to JSON? It is already a string? – richin Commented Mar 7, 2019 at 10:49
  • 1 Your buffer variable is already a string! – Krishna Prashatt Commented Mar 7, 2019 at 10:49
  • I am trying to convert that 'string buffer value' from a buffer to a string. – Joe Commented Mar 7, 2019 at 11:40
  • Added example when is working and I need to do the same but starting from a String... – Joe Commented Mar 7, 2019 at 11:43
Add a comment  | 

4 Answers 4

Reset to default 13

Automatically converted when concatenated with an empty string:

console.log('' + bufferOne)

No native way for that, but I wrote a sample method for you:

function bufferFromBufferString(bufferStr) {
    return Buffer.from(
        bufferStr
            .replace(/[<>]/g, '') // remove < > symbols from str
            .split(' ') // create an array splitting it by space
            .slice(1) // remove Buffer word from an array
            .reduce((acc, val) => 
                acc.concat(parseInt(val, 16)), [])  // convert all strings of numbers to hex numbers
     )
}

result:

const newBuffer = bufferFromBufferString('<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>')
> newBuffer
<Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>
> newBuffer.toString()
'This is a buffer example.'

Another method of achieving this:

function toBuffer(bufferString) {
  const hex = bufferString.match(/\s[0-9a-fA-F]+/g).map((x) => x.trim());
  return Buffer.from(hex.join(''), 'hex');
}

const buffer = '<Buffer 49 20 6c 6f 76 65 20 79 6f 75>';
const actualBuffer = toBuffer(buffer);

console.log(buffer); // <Buffer 49 20 6c 6f 76 65 20 79 6f 75>
console.log(actualBuffer); // <Buffer 49 20 6c 6f 76 65 20 79 6f 75>
console.log(actualBuffer === buffer); // false
console.log(actualBuffer.toString()); // Secret message

Same function, different syntax:

const toBuffer = (bufferString) =>
  Buffer.from(
    bufferString
      .match(/\s[0-9a-fA-F]+/g)
      .map((x) => x.trim())
      .join(''),
    'hex'
  );

you can not make a buffer template, but immediately give an array of numbers if you took them from somewhere.

At least you're making it harder.

const ListToBuffer = (list) => Buffer.from(list.join(''), 'hex');
发布评论

评论列表(0)

  1. 暂无评论