Node.js how to define a buffer object i have - node.js

So im trying to try stuff with buffers i opened a file in buffer mode then i copied it, but how do i define it back in a variable? Like so i can give the buffer object to someone else and they can assign it as buffer and use it normally i tried googling around and testing stuff. Hope someone can help if you didn't understand anythin feel free to ask.
More details:
const fs = require("fs");
const f =
fs.readFileSync("path/to/file.txt")
f
// now a buffer
// copied buffer and kept it
// now how do i turn it back into a buffer object for later use?
For example i have
<Buffer 53 45 43 52 45 54 20 4d 45 53 53 41 47
45 20 50 41 53 53 57 4f 52 44 20 3d 0a 44 72 6d
43 6b 4c 38 56 71 45 76 48 4f 70 6c 4d 61 77 52
59 33 4e 31 6d ... >
How would i enter it to node? If i just pasted it it as it is or as string won't detect it as a buffer dunno if this is possible

Use Buffer.from(string);
const fs = require("fs");
const f = fs.readFileSync("path/to/file.txt");
let st = f.toString();
let new_f = new Buffer.from(st);
//Now new_f is the buffer object 'f' you got from reading the file.

Related

Can't convert utf8 buffer into ISO8859_1 buffer without data loss

As an example, I'm trying to convert the following utf8 string into ISO8859_1
String = Plœmeurl’
The String's hex representation: 50 6c c5 93 6d 65 75 72 6c e2 80 99 20
After iconv-lite conversion to ISO8859_1:
50 6c 3f 6d 65 75 72 6c 3f 20
The hex value once inserted in my ISO8859_1 database looks like this:
Code:
var iconv = require('iconv-lite');
iconv.encode(Buffer.from(`Plœmeurl’ `, 'utf8'), "ISO-8859-1")
Am I doing something silly ?

Unexpected result when using the hex encoding on Buffer object

All private keys listed have been changed and they are addresses on a test network anyway so don't get any ideas!
The issue:
I'm attempting to use the Hex encoding of a Buffer object, however, when I try and use the Buffer.from(key, 'hex') function, I get an empty Buffer object back. Anytime I omit the Hex encoding, the Buffer object acts normally.
Any ideas as to what the root of the issue is?
const privateKey1 = Buffer.from('0x6ee345295b6b2a6df6e06ba3bb5f09a301acf5b1af3c23452025373e15fdb230', 'hex')
console.log(privateKey1)
The above console.log returns <Buffer >
const privateKey2 = Buffer.from('0x6ee88295b6b2a6df6e06ea3ab5f09a401acf5b1af3c75672025373e15edb230')
console.log(privateKey2)
The above console.log returns <Buffer 30 78 36 65 65 38 37 32 32 39 35 62 36 62 32 61 36 64 66 36 65 30 36 63 61 33 61 62 35 66 30 39 61 34 30 31 61 63 66 35 62 31 61 66 33 63 37 38 33 30 ... 16 more bytes>
I also run into the same problem
The way I resolved it was to use a substring to get rid of the 0x value that is at the beginning of the private key
let private_key = '0x6ee88295b6b2a6df6e06ea3ab5f09a401acf5b1af3c75672025373e15edb230';
const privateKey2 = Buffer.from(private_key.substring(2) , 'hex')

Convert "Buffer 64 61 74 61 3a 69 6d 61 67 65 2f" to img src in react

I am working on retrieving the image data url from postgresql database and convert back to img src in react. However the below code is not work and the base64 string is kind of strange when console log.
const bufs = Buffer.from(returnUser[0].img);
const base64 = bufs.toString("base64");
res.json({
userName: returnUser[0].username,
id: returnUser[0].id,
userImg: `data:image/jpeg;base64,${base64}`,
});
In nodejs, the data I got back from database returnUser[0].img is like <Buffer 64 61 74 61 3a 69 6d 61 67 65 2f 6a 70 65 67 3b 62 61 73 65 36 34 2c 2f 39 6a 2f 34 41 41 51 53 6b 5a 4a 52 67 41 42 41 51 45 41 53 41 42 49 41 41 44 ... 67141 more bytes>
I console the base64 is like ZGF0YTppbWFnZS9qcGVnO2Jhc2U2NCwvOWovNEF... Then I copy all the base64 stuff to the online decode website. https://base64.guru/converter/decode/file. The result coming back is what I want: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gOTIK/9sAQwAG...
The above result I copy to the react image, it works.
But I don't know how I can get this result in nodejs. Are there any code I am missing?
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBG...
Thanks so much for the answers!

Hi, I want to know how can we insert binary data between a binary string in python 3.7?

So, here is something I want to do;
binary_string_data_input = '00 AE 65 67 32 2F 33 3A 54 20 46'
and I want to insert the following bytes BETWEEN the string like this:
binary_string_data_output = '00 AE 65 67 **55 06 33 55 FF** 32 2F 33 3A 54 20 46'
Help would be much appreciated thanks!
You can split (or slice) to get the first part:
>>> string_data_input[:12]
'00 AE 65 67 '
and similarly the last part:
>>> string_data_input[12:]
'32 2F 33 3A 54 20 46'
So, you can join these with your new "inner" part:
>>> string_data_input[:12] + '55 06 33 55 FF ' + string_data_input[12:]
'00 AE 65 67 55 06 33 55 FF 32 2F 33 3A 54 20 46'

how to get textual stdout from console.log(require("child_process").execSync('ls'))?

How to print the stdout from console.log(require("child_process").execSync('ls'))
I tried in ts
import { execSync } from 'child_process';
console.log(execSync('ls -la'));
which was then compiled to js:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
console.log(child_process_1.execSync('ls -la'));
but when I run it I get only like buffer how to get the stdout?
$ node app.js
$ <Buffer 74 6f 74 61 6c 20 38 38 0a 64 72 77 78 72 2d 78 72 2d 78 20 20 31 31 20 74 6f 6d 65 72 2e 62 65 6e 64 61 76 69 64 20 20 73 74 61 66 66 20 20 20 20 33 ... >
What am i missing? how to get the textual stdout?
Your last line should be:
console.log(child_process_1.execSync('ls -la').toString());
execSync returns a buffer, simply call toString on the buffer to get the contents of the buffer as a string.

Resources