string to character code hex string

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 2 10:41:34 PDT 2017


On 09/02/2017 10:07 AM, lithium iodate wrote:

 >> Lazy version, which the user can easily generate a string from by
 >> appending .array:
 >>
 >> import std.stdio;
 >>
 >> auto hexString(R)(R input) {
 >>     import std.conv : text;
 >>     import std.string : format;
 >>     import std.algorithm : map, joiner;
 >>     return input.map!(c => format("%02x", c)).joiner;
 >> }
 >>
 >> void main() {
 >>     writeln("AAA".hexString);
 >> }
 >>
 >> To generate string:
 >>
 >>     import std.range : array;
 >>     writeln("AAA".hexString.array);
 >>
 >> Ali
 >
 > Please correct my if i'm wrong, but it think this has issues regarding
 > unicode.
 > "ö…" becomes "f62026", which, interpreted as UTF-8, is a control
 > character ~ " &", so you either need to add padding or use ..byCodeUnit
 > so it becomes "c3b6e280a6" (correct UTF-8) instead.

You're right but I think there is no intention of interpreting the 
result as UTF-8. "f62026" is just to be used as "f62026", which can be 
converted byte-by-byte back to "ö…". That's how understand the 
requirement anyway.

Ali



More information about the Digitalmars-d-learn mailing list