Discussion:
how to get the \uxxxx unicode code from a char
jicman via Digitalmars-d-learn
2014-10-14 19:46:57 UTC
Permalink
Greetings.

Imagine this code,

char[] s = "ABCabc";
foreach (char c; s)
{
// how do I convert c to something an Unicode code? ie. \u9999.

}

thanks.
Sean Kelly via Digitalmars-d-learn
2014-10-14 19:49:15 UTC
Permalink
Post by jicman via Digitalmars-d-learn
Greetings.
Imagine this code,
char[] s = "ABCabc";
foreach (char c; s)
{
// how do I convert c to something an Unicode code? ie.
\u9999.
}
I'd look at the JSON string encoder.
jicman via Digitalmars-d-learn
2014-10-14 20:03:36 UTC
Permalink
Post by Sean Kelly via Digitalmars-d-learn
Post by jicman via Digitalmars-d-learn
Greetings.
Imagine this code,
char[] s = "ABCabc";
foreach (char c; s)
{
// how do I convert c to something an Unicode code? ie.
\u9999.
}
I'd look at the JSON string encoder.
JSON? What does JSON has to do with my basic D? :-) No thanks.
:-)
Brad Anderson via Digitalmars-d-learn
2014-10-14 20:05:05 UTC
Permalink
Post by jicman via Digitalmars-d-learn
Post by Sean Kelly via Digitalmars-d-learn
Post by jicman via Digitalmars-d-learn
Greetings.
Imagine this code,
char[] s = "ABCabc";
foreach (char c; s)
{
// how do I convert c to something an Unicode code? ie.
\u9999.
}
I'd look at the JSON string encoder.
JSON? What does JSON has to do with my basic D? :-) No thanks.
:-)
Sean's saying that the JSON encoder does the same thing so you
can look there for how to do it.

https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L579
Brad Anderson via Digitalmars-d-learn
2014-10-14 20:08:01 UTC
Permalink
Post by Brad Anderson via Digitalmars-d-learn
https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L579
Oops. Linked the the parser section. I actually don't see any
unicode escape encoder in here. Perhaps he meant the upcoming
JSON module.
Brad Anderson via Digitalmars-d-learn
2014-10-14 20:13:16 UTC
Permalink
On Tuesday, 14 October 2014 at 20:05:07 UTC, Brad Anderson
Post by Brad Anderson via Digitalmars-d-learn
https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L579
Oops. Linked the the parser section. I actually don't see any
unicode escape encoder in here. Perhaps he meant the upcoming
JSON module.
Here we go.

https://github.com/s-ludwig/std_data_json/blob/4ecb90626055269f4897902404741f1173fb5e8e/source/stdx/data/json/generator.d#L451

Sönke's is pretty sophisticated. You could probably just use the
non-surrogate supporting simple branch.
jicman via Digitalmars-d-learn
2014-10-14 20:28:17 UTC
Permalink
On Tuesday, 14 October 2014 at 20:08:03 UTC, Brad Anderson
On Tuesday, 14 October 2014 at 20:05:07 UTC, Brad Anderson
Post by Brad Anderson via Digitalmars-d-learn
https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L579
Oops. Linked the the parser section. I actually don't see any
unicode escape encoder in here. Perhaps he meant the upcoming
JSON module.
Here we go.
https://github.com/s-ludwig/std_data_json/blob/4ecb90626055269f4897902404741f1173fb5e8e/source/stdx/data/json/generator.d#L451
Sönke's is pretty sophisticated. You could probably just use
the non-surrogate supporting simple branch.
thanks. the problem is that I am still in D1. ;-) over 300K
lines of code and growing...
Sean Kelly via Digitalmars-d-learn
2014-10-16 19:40:57 UTC
Permalink
On Tuesday, 14 October 2014 at 20:05:07 UTC, Brad Anderson
Post by Brad Anderson via Digitalmars-d-learn
https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L579
Oops. Linked the the parser section. I actually don't see any
unicode escape encoder in here. Perhaps he meant the upcoming
JSON module.
Wow... the current std.json doesn't do string encoding? I knew
it was bad, but...

In any case, yes, I mentioned JSON because strings are supposed
to be encoded exactly the way you're asking. It was easier to
point at that than try to outline the process explicitly.

ketmar via Digitalmars-d-learn
2014-10-14 19:56:18 UTC
Permalink
On Tue, 14 Oct 2014 19:46:57 +0000
Post by jicman via Digitalmars-d-learn
char[] s = "ABCabc";
foreach (char c; s)
{
// how do I convert c to something an Unicode code? ie. \u9999.
}
string res;
foreach (dchar ch; s) res ~= "\\u%04X".format(ch);
// here we have the result in res

note "dchar" instead of "char". with "dchar" foreach() does utf-8
decoding (as 's' is in utf-8).
jicman via Digitalmars-d-learn
2014-10-14 20:18:47 UTC
Permalink
On Tuesday, 14 October 2014 at 19:56:29 UTC, ketmar via
Post by ketmar via Digitalmars-d-learn
On Tue, 14 Oct 2014 19:46:57 +0000
jicman via Digitalmars-d-learn
Post by jicman via Digitalmars-d-learn
char[] s = "ABCabc";
foreach (char c; s)
{
// how do I convert c to something an Unicode code? ie.
\u9999.
}
string res;
foreach (dchar ch; s) res ~= "\\u%04X".format(ch);
// here we have the result in res
note "dchar" instead of "char". with "dchar" foreach() does
utf-8
decoding (as 's' is in utf-8).
Thanks. This is a missing function in std.uni or std.string.

josé
Ali Çehreli via Digitalmars-d-learn
2014-10-14 20:39:58 UTC
Permalink
Post by jicman via Digitalmars-d-learn
Thanks. This is a missing function in std.uni or std.string.
josé
I don't know D1 but there is std.utf.decode:

http://dlang.org/phobos/std_utf.html#decode

Ali
Loading...