string to character code hex string

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 2 11:07:51 PDT 2017


On Saturday, 2 September 2017 at 17:45:30 UTC, Moritz Maxeiner 
wrote:
> 
> If this (unnecessary waste) is of concern to you (and from the 
> fact that you used ret.reserve I assume it is), then the easy 
> fix is to use `sformat` instead of `format`:
>

Yes, thanks. I'm going to go with a variation of your approach:

private
string toAsciiHex(string str)
{
     import std.ascii : lowerHexDigits;
     import std.exception: assumeUnique;

     auto ret = new char[str.length * 2];
     int i = 0;

     foreach(c; str) {
         ret[i++] = lowerHexDigits[(c >> 4) & 0xF];
         ret[i++] = lowerHexDigits[c & 0xF];
     }

     return ret.assumeUnique;
}

I'm not sure how the compiler would mangle UTF8, but I intend to 
use this on one specific function (actually the 100's of 
instantiations of it). It will predictably named though.

    Thanks!




More information about the Digitalmars-d-learn mailing list