On Saturday, 2 September 2017 at 16:52:17 UTC, Ali Çehreli wrote:
On 09/02/2017 09:23 AM, bitwise wrote:
On Saturday, 2 September 2017 at 15:53:25 UTC, bitwise wrote:
[...]
This seems to work well enough.
string toAsciiHex(string str)
{
import std.array : appender;
auto ret = appender!string(null);
ret.reserve(str.length * 2);
foreach(c; str) ret.put(format!"%x"(c));
return ret.data;
}
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.