utf8.js | Convert JavaScript strings to a UTF8 byte array
kandi X-RAY | utf8.js Summary
kandi X-RAY | utf8.js Summary
Convert JavaScript strings to a UTF8 byte array
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of utf8.js
utf8.js Key Features
utf8.js Examples and Code Snippets
Community Discussions
Trending Discussions on utf8.js
QUESTION
I know many people will say this has already been answered like so https://stackoverflow.com/a/4983999/1833322 But let me explain why it's not just as straight forwarded..
I would like to use PHP to convert something "that looks like ascii" into "utf-8"
There is a website which does this https://onlineutf8tools.com/convert-ascii-to-utf8
When i input this string Z…Z
i get back Z⬦Z
which is the correct output.
I tried iconv
and some mb_
functions. Though i can't figure out if these functions are capable of doing what i want or which options that i need. If it's not possible with these functions some self-written PHP code would be appreciated. (The website runs javascript and i don't think PHP i less capable in this regard)
To be clear: the goal is to recreate in PHP what that website is doing. Not to have a semantic debate about ascii and utf-8
EDIT: the website uses https://github.com/mathiasbynens/utf8.js which says
it can encode/decode any scalar Unicode code point values, as per the Encoding Standard.
Standard linking to https://encoding.spec.whatwg.org/#utf-8 So this library says it implements the standard, then what about PHP ?
...ANSWER
Answered 2020-May-02 at 11:59UTF-8 is a superset of ASCII so converting from ASCII to UTF-8 is like converting a car into a vehicle.
QUESTION
We all know UTF-8 is hard. I exported my messages from Facebook and the resulting JSON file escaped all non-ascii characters to unicode code points.
I am looking for an easy way to unescape these unicode code points to regular old UTF-8. I also would love to use PowerShell.
I tried
...ANSWER
Answered 2018-Jun-13 at 13:14The Unicode code point of the character is U+1F44D
.
Using the variable-length UTF-8 encoding, the following 4 bytes (expressed as hex. numbers) are needed to represent this code point: F0 9F 91 8D
.
While these bytes are recognizable in your string,
QUESTION
I have been trying to understanding why the need for encoding/decoding to UTF-8 happens all over the place in JavaScript land, and learned that JavaScript uses UTF-16 encoding.
Let’s talk about Javascript string encoding
So I'm assuming that's why a library such as utf8.js exists, to convert between UTF-16 and UTF-8.
But then at the end he provides some insights:
Encoding in Node is extremely confusing, and difficult to get right. It helps, though, when you realize that Javascript string types will always be encoded as UTF-16, and most of the other places strings in RAM interact with sockets, files, or byte arrays, the string gets re-encoded as UTF-8.
This is all massively inefficient, of course. Most strings are representable as UTF-8, and using two bytes to represent their characters means you are using more memory than you need to, as well as paying an O(n) tax to re-encode the string any time you encounter a HTTP or filesystem boundary.
That reminded me of the in the HTML
, which I never really thought too much about, other than "you need this to get text working properly".
Now I'm wondering, which this question is about, if that tag tells JavaScript to do UTF-8 encoding. That would then mean that when you create strings in JavaScript, they would be UTF-8 encoded rather than UTF-16. Or if I'm wrong there, what exactly it is doing. If it is telling JavaScript to use UTF-8 encoding instead of UTF-16 (which I guess would be considered the "default"), then that would mean you don't need to be paying that
O(n)
tax over doing conversions between UTF-8 and UTF-16, which would mean a performance improvement. Wondering if I am understanding correctly, or if not, what I am missing.
ANSWER
Answered 2018-Jul-23 at 22:59Charset in meta
The tag tells HTML (less sloppily: the HTML parser) that the encoding of the page is utf8.
JS does not have a built-in facility to switch between different encondings of strings - it is always utf-16.
Asymptotic bounds
I do not think that there is a O(n)
penalty for encoding conversions. Whenever this kind of encoding change is due, there already is an O(n)
operation: reading/writing the data stream. So any fixed number of operations on each octet would still be O(n)
. Encoding change requires local knowledge only, ie. a look-ahead window of fixed length only, and can thus be incorporated in the stream read/write code with a penalty of O(1)
.
You could argue that the space penalty is O(n)
, though if there is the need to store the string in any standard encoding (ie. without compression), the move to utf-16 means a factor of 2 at max thus staying within the O(n)
bound.
Constant factors
Even if the concern is minimizing the constant factors hidden in O(n)
notation encoding change have a modest impact, in the time domain at least. Writing/reading a utf-16 stream as utf-8 for the most part of (Western) textual data means skipping every second octet / inserting null octets. That performance hit pales in comparison with the overhead and the latency stemming from interfacing with a socket or the file system.
Storage is different, of course, though storage is comparatively cheap today and the upper bound of 2 still holds. The move from 32 to 64 bit has a higher memeory impact wrt to number representations and pointers.
QUESTION
I faced a weird problem today after trying to decode a utf8 formatted string. It's being fetched through stream as an array of strings but formatted in utf8 somehow (I'm using fast-csv). However as you can see in the console if I log it directly it shows the correct version but when it's inside an object literal it's back to utf8 encoded version.
...ANSWER
Answered 2018-Jul-07 at 17:15JavaScript uses UTF-16 for Strings. It also has a numeric escape notation for a UTF-16 code unit. So, when you see this output in your debugger
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install utf8.js
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page