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 ?
Related
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!
const cheerio = require('cheerio');
const request = require('sync-request');
const fs = require('fs');
var res = request('GET', 'https://edition.cnn.com/');
console.log(res.getBody())
This is my code. I want to get HTML code of https://edition.cnn.com/. But it returning following:
<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 3c 68 74 6d 6c 20 63 6c 61 73 73 3d 22 6e 6f 2d 6a 73 22 3e 3c 68 65 61 64 3e 3c 6d 65 74 61 20 63 6f 6e ... >
You are receiving a buffer currently, you need to convert it to string. Use this -
console.log(res.getBody().toString())
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.
I am getting a telnet response as showed below when tried with nodejs code. Actually it is in xml format. When I did directly call the telnet response, the correct xml format is delivering. Can someone plz help, why 'Buffer' response when calling via nodejs.
"Buffer 3c 6e 66 6c 2d 65 76 65 6e 74 3e 0d 0a 20 3c 67 61 6d 65 63 6f 64 65 20
3 6f 64 65 3d 22 32 30 31 37 31 31 33 30 30 30 36 22 20 67 6c 6f 62 61 6c 2d .. "
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.