UCS-2 is a character encoding standard in which characters are represented by a fixed-length 16 bits (2 bytes)
With the encoding known you can decode properly and get the data as expected:
const data = [79, 0, 98, 0, 115, 0, 116, 0, 59, 0, 80, 0, 101, 0, 114, 0, 115, 0, 111, 0, 110, 0, 59, 0]
const dataBuffer = Buffer.from(data)
const utf16Decoder = new TextDecoder('UTF-16')
console.log(utf16Decoder.decode(dataBuffer)) // Obst;Person;
I think it's trying to decode as utf8 by default and getting upset at the unrenderable null bytes between every character:

node.js - Javascript - How to convert buffer to a string? - Stack Overflow
node.js - Converting a nodejs buffer to string and back to buffer gives a different result in some cases - Stack Overflow
node.js - Convert buffer to readable string javascript - Stack Overflow
converting buffer to string - javascript
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.'
Your code is working. The buffer you have is actually the string "[object Object]".
let b = Buffer.from('[object Object]', 'utf8')
console.log(JSON.stringify(b))
// {"type":"Buffer","data":[91,111,98,106,101,99,116,32,79,98,106,101,99,116,93]}
console.log(b.toString('utf8'))
// [Object object]
The problem you need to figure out is why is a buffer with that string being sent. It seems like the sender of the buffer needs to call stringify or otherwise serialize the object before sending it. Then you can turn it back to a string with toString() and use JSON.parse() on the string.
Try
console.log(Buffer.from(val).toString());
This will convert [object Object] as a string
You should pass the encoding to the .toString() function like in thew following example:
foo.toString('utf8');
Try this :
let data = {"buffer": {
"type": "Buffer",
"data": [
8,
4,
18,
32,
203,
243,
203,
190,
254,
80,
185,
227,
225,
187,
114,
219,
243,
87,
83,
254,
248,
95,
139,
1,
26,
198,
149,
158,
30,
44,
146,
237,
166,
70,
199,
9,
26,
32,
138,
251,
102,
14,
230,
244,
102,
250,
142,
53,
210,
190,
9,
23,
169,
98,
14,
156,
148,
99,
168,
204,
145,
108,
191,
223,
189,
58,
202,
61,
52,
254
]
}}
let newstr = JSON.stringify(data);
console.log(newstr)
console.log(JSON.parse(newstr).buffer.data)