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

tetyys tetyys at tetyys.com
Mon Feb 5 09:45:11 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;
>
> Then get an error:
> core.exception.UnicodeException at src\rt\util\utf.d(292):invalid 
> UTF-8 sequence
> --------------
> 0x0041B8E6
> 0x00419530
> 0x0040796F
>
> Thanks.

Casting unknown bytes to string or char is unsafe, and obviously 
some bytes can be invalid UTF8 sequences.


More information about the Digitalmars-d-learn mailing list