How to convert hex string to string or ubytes? Thanks.

Seb seb at wilzba.ch
Mon Feb 5 10:04:10 UTC 2018


On Monday, 5 February 2018 at 08:41:43 UTC, FrankLike wrote:
> On Monday, 5 February 2018 at 06:12:22 UTC, H. S. Teoh wrote:
>> On Mon, Feb 05, 2018 at 05:48:00AM +0000, FrankLike via
>
>> 	auto input = "48656c6c6f20776f726c6421";
>> 	auto str = input.chunks(2)
>> 	                .map!(digits => cast(char) 
>> digits.to!ubyte(16))
>> 			.array;
>
> But,if the input come from here:
>
> import std.digest.md;
> auto hash =md5Of("Some Words");
> auto input = cast(string)hexString(hash);
>
>  auto str = input.chunks(2)
>  	         .map!(digits => cast(char) digits.to!ubyte(16))
>  		 .array;
>

md5Of already returns an ubyte[] array.

Use toHexString to get the string:

https://dlang.org/phobos-prerelease/std_digest.html#.toHexString

Try it here:

---
import std.algorithm, std.range, std.stdio, std.conv, std.digest, 
std.digest.md, std.range;
"foo".md5Of.writeln;
"foo".md5Of.toHexString.writeln;
"foo".md5Of.toHexString!(LetterCase.lower).writeln;
"foo".md5Of.toHexString[].chunks(2).map!(a => 
a.to!ubyte(16)).writeln;
---

https://run.dlang.io/is/zoH7fE


More information about the Digitalmars-d-learn mailing list