In Javascript, how can I convert BMP unicode characters to binary (and back)?
I can't seem to find any built-in string method binaryCharCodeAt()
does something like that exist?
If not, my guess as to how to do it manually would be to create an array containing for example [00001111], [00001110], [00001100]
and so on...
Then to get binary, I could do myArray[String.charCodeAt(j)]
Then to go from binary to unicode, I could search the array for a binary string, returning its position in the array, and put that into String.fromCharCode()
In this case, these binary codes are arbitrarily assigned, and arent the correct ones for each character. But thats ok.. (although correct would be preferred) I just need any binary.
The problem I foresee is, to search an array containing 65000+ items, hundreds or thousands of times, could end up costing a lot of processing time.
So, is there any pre-existing method or library, or can you suggest a better way to do this manually?
In Javascript, how can I convert BMP unicode characters to binary (and back)?
I can't seem to find any built-in string method binaryCharCodeAt()
does something like that exist?
If not, my guess as to how to do it manually would be to create an array containing for example [00001111], [00001110], [00001100]
and so on...
Then to get binary, I could do myArray[String.charCodeAt(j)]
Then to go from binary to unicode, I could search the array for a binary string, returning its position in the array, and put that into String.fromCharCode()
In this case, these binary codes are arbitrarily assigned, and arent the correct ones for each character. But thats ok.. (although correct would be preferred) I just need any binary.
The problem I foresee is, to search an array containing 65000+ items, hundreds or thousands of times, could end up costing a lot of processing time.
So, is there any pre-existing method or library, or can you suggest a better way to do this manually?
Share Improve this question asked Apr 25, 2012 at 16:04 monkey blotmonkey blot 9853 gold badges10 silver badges18 bronze badges 3- what do you mean by BMP unicode characters? – Esailija Commented Apr 25, 2012 at 16:16
- 1 @Esailija, en.wikipedia/wiki/Plane_(Unicode)#Basic_Multilingual_Plane – monkey blot Commented Apr 25, 2012 at 16:20
-
And what's wrong with
String.fromCharCode( "a".charCodeAt(0) )
. – Esailija Commented Apr 25, 2012 at 16:24
2 Answers
Reset to default 3Do note that it is not totally correct to say "to binary and back", because unicode characters do not need to have a unique binary representation (it depends on the encoding, e.g. UTF-8). However I believe most of the UTF-... encodings are backwards-patible with each other in terms of binary encodings.
But since you stated that you don't care what encoding you are using, you can do exactly as Kolink said (his answer was improperly downvoted, but was also not plete):
edit: As Esailija points out, the OP was only interested in basic multilingual plane characters, which only have one codepoint. The below code is overkill, though will still work on both BMP and non-BMP codepoints.
"some string".charCodeAt
gives you the hex of the codepoints of some encoding. In my case it is UTF-16:
"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745659031a4638720.html
评论列表(0条)