admin管理员组文章数量:1026989
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:
"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
Add a ment
|
2 Answers
Reset to default
3
Do 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:
"
本文标签:
javascriptunicode to binaryStack Overflow
版权声明:本文标题:javascript - unicode to binary? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://it.en369.cn/questions/1745659029a2161774.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论