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:

Assigning Buffer to string issue
"utf8" should be given as an argument to fs.readFileSync, but you have it as an argument to path.join. Do this instead:
const configDataRaw = fs.readFileSync( path.join(__dirname, "../../../configuration.json"), "utf8" );
Providing an encoding argument to readFileSync (in this case the encoding is "utf8") causes TypeScript to apply an overloaded signature of readFileSync where the return type is string instead of Buffer. At runtime the encoding argument causes Node to convert the file content buffer to a string for you automatically.
node.js - Javascript - How to convert buffer to a string? - Stack Overflow
node.js - Convert buffer to readable string javascript - Stack Overflow
node.js - Convert string to buffer Node - Stack Overflow
// doesn't work
const configDataRaw = fs.readFileSync(
path.join(__dirname, "../../../configuration.json", "utf8")
);
const configData = JSON.parse(configDataRaw);
// const configDataRaw: Buffer
// Argument of type 'Buffer' is not assignable to parameter of type 'string'.ts(2345)
// works
const configData2 = JSON.parse(path.join(__dirname,
"../../../configuration.json", "utf8")
);All of the following failed:
const configDataRaw as string = fs.readFileSync(
const configDataRaw<string> = fs.readFileSync(
const configData = JSON.parse((configDataRaw as string));
const configData = JSON.parse(configDataRaw<string>);What is the correct way to assert this is not a buffer, but a string?
In the first code block, code sample #2, it seems to infer properly, I don't understand why the more iterative version acts differently here.
"utf8" should be given as an argument to fs.readFileSync, but you have it as an argument to path.join. Do this instead:
const configDataRaw = fs.readFileSync(
path.join(__dirname, "../../../configuration.json"),
"utf8"
);
Providing an encoding argument to readFileSync (in this case the encoding is "utf8") causes TypeScript to apply an overloaded signature of readFileSync where the return type is string instead of Buffer. At runtime the encoding argument causes Node to convert the file content buffer to a string for you automatically.
Type assertions go after the value.
If this is a JSON file, just use require or import.
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 can do:
var buf = Buffer.from(bufStr, 'utf8');
But this is a bit silly, so another suggestion would be to copy the minimal amount of code out of the called function to allow yourself access to the original buffer. This might be quite easy or fairly difficult depending on the details of that library.
You can use Buffer.from() to convert a string to buffer. More information on this can be found here
var buf = Buffer.from('some string', 'encoding');
for example
var buf = Buffer.from(bStr, 'utf-8');