sonicdoe / detect-character-encoding

Detect character encoding using ICU
Other
84 stars 15 forks source link

Detect encoding in variable #17

Closed KaKi87 closed 5 years ago

KaKi87 commented 5 years ago

Hello.

I am wondering if it is possible to directly convert a string to UTF8 without saving it in a file ?

Thanks.

sonicdoe commented 5 years ago

Are you looking to directly detect the encoding of a variable or to directly convert a string to UTF-8? As for the latter, I’m not sure how detect-character-encoding can be of much help.

sonicdoe commented 5 years ago

Closing due to lack of response.

KaKi87 commented 5 years ago

Hey, I wanted to do both, and it would have been great if your module could do the first thing, but I was actually downloading a file and the encoding was specified in the HTTP response header, I didn't knew it before I posted that issue. Thanks !

sonicdoe commented 5 years ago

Using the HTTP header is definitely a better idea but just in case you might need it again: You can download a file to a Buffer which you can then directly pass to detect-character-encoding.

KaKi87 commented 5 years ago

Thanks !

KaKi87 commented 5 years ago

The Buffer.from method takes encoding as second parameter, and it is default to UTF-8, but I don't know the encoding, and that's why I would like to use your library, then how could I pass a variable to Buffer without knowing the encoding and then detect it using your library ? https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding

sonicdoe commented 5 years ago

Buffer.from(array) accepts an array of octets which you could then pass to detect-character-encoding. If you’d like to download a file, though, various HTTP request libraries (such as Got) already support returning a Buffer.