| Paste number 60982: | [erlang] unicode string to javascript \uXXXX |
| Pasted by: | b101 |
| When: | 1 year, 1 month ago |
| Share: | Tweet this! | http://paste.lisp.org/+1B1Y |
| Channel: | None |
| Paste contents: |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Based on query discussed on #erlang,inputs from smoove,stonecypher.
%%
%% To be able to convert unicode to a form that can be evaluated
%% in javascript since it of unicode escape format
%% eg: the copyright symbol will be escaped into \u00a9
%%
%%
%%%%
%% Resources for unicode / binary/ string conversion in erlang
%%%%
%% http://pianosa.googlecode.com/svn/trunk/erlang/utf8.erl
%% http://12monkeys.co.uk/starling/
%% http://www.lshift.net/~tonyg/erlang-rfc4627/
%% http://www.erlang.org/doc/man/io.html
%% http://developer.mozilla.org/en/docs/International_characters_in_XUL_JavaScript
%%%%
%% -b101
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%functions added to utf8.erl which already exports [encode/1, decode/1]
-export([list_to_uXXXX/1,binary_to_uXXXX/1]
%list into unicode escaped javascript form
list_to_uXXXX(Str)->
lists:flatten[io_lib:format("\\u~4.16.0B",[X]) || X <- Str].
%% first converting to list,then to uXXXX form
binary_to_uXXXX(Bin)->
list_to_uXXXX(decode(Bin)).
Annotations for this paste:
| Annotation number 1: | update |
| Pasted by: | b101 |
| When: | 1 year, 1 month ago |
| Share: | Tweet this! | http://paste.lisp.org/+1B1Y#1 |
| Paste contents: |
UPDATES
%%%%%%%%
found that suppose you have unicode rendered within javascript, it can very much be used as a literal, and will infact match identically to the same unicode word found elsewhere within the HTML DOM.
also, even though the to_XXXX did no throw any errors, it wasnt showing the correct possible because N number of binaries/list does not necessarily mean N symbols (a symbol can be formed by more than a series of binary elements)
an excellent utility that helped me easily evalute the decimal/binary/hexa values of unicode symbols (just paste them into a text area to see results) was located at http://software.ellerton.net/txt2bin/
~b101