Thread overview
How to convert hex string to string or ubytes? Thanks.
Feb 05, 2018
FrankLike
Feb 05, 2018
H. S. Teoh
Feb 05, 2018
H. S. Teoh
Feb 05, 2018
FrankLike
Feb 05, 2018
FrankLike
Feb 05, 2018
FrankLike
Feb 05, 2018
tetyys
Feb 05, 2018
FrankLike
Feb 05, 2018
Seb
Feb 05, 2018
FrankLike
February 04, 2018
On Mon, Feb 05, 2018 at 05:48:00AM +0000, FrankLike via Digitalmars-d-learn wrote:
> Now,I can get the string from hex string in compile time,but how to get it in run time?
> 
> How to get it in run time?
> 
> Thanks.

	import std.conv;
	string hex = "900D1DEA";
	uint value = hex.to!uint(16);
	assert(value == 0x900D1DEA);


T

-- 
Be in denial for long enough, and one day you'll deny yourself of things you wish you hadn't.
February 05, 2018
Now,I can get the string from hex string in compile time,but how to get it in run time?

How to get it in run time?

Thanks.
February 04, 2018
On Mon, Feb 05, 2018 at 05:48:00AM +0000, FrankLike via Digitalmars-d-learn wrote:
> Now,I can get the string from hex string in compile time,but how to get it in run time?
> 
> How to get it in run time?
[...]

Oh wait, I think I misunderstood your original question. Perhaps this is closer to what you want:

	import std.algorithm;
	import std.array;
	import std.conv;
	import std.range;

	auto input = "48656c6c6f20776f726c6421";
	auto str = input.chunks(2)
	                .map!(digits => cast(char) digits.to!ubyte(16))
			.array;
	assert(str == "Hello world!");


T

-- 
Study gravitation, it's a field with a lot of potential.
February 05, 2018
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

> 	assert(str == "Hello world!");

Thanks.very good!


February 05, 2018
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 = hexString(hash);
 auto str = input.chunks(2)
 	         .map!(digits => cast(char) digits.to!ubyte(16))
 		 .array;

Then get an error:
core.exception.UnicodeException@src\rt\util\utf.d(292):invalid UTF-8 sequence
--------------
0x0041B8E6
0x00419530
0x0040796F

Thanks.

February 05, 2018
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@src\rt\util\utf.d(292):invalid UTF-8 sequence
--------------
0x0041B8E6
0x00419530
0x0040796F

Thanks.

February 05, 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@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.
February 05, 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
February 05, 2018
On Monday, 5 February 2018 at 09:45:11 UTC, tetyys wrote:
> On Monday, 5 February 2018 at 08:41:43 UTC, FrankLike wrote:

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

Thank you.I got my error.


February 05, 2018
On Monday, 5 February 2018 at 10:04:10 UTC, Seb wrote:
> On Monday, 5 February 2018 at 08:41:43 UTC, FrankLike wrote:

>> auto input = cast(string)hexString(hash);
> Use toHexString to get the string:

Sorry,'hexString(hash)' is my clerical error.

Thank you.I got the answer "no array".