digitalmars.D.learn - How to convert hex string to string or ubytes? Thanks.
- FrankLike (4/4) Feb 04 2018 Now,I can get the string from hex string in compile time,but how
- H. S. Teoh (8/14) Feb 04 2018 import std.conv;
- H. S. Teoh (16/20) Feb 04 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.
Feb 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.
Feb 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.
Feb 04 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 viaassert(str == "Hello world!");Thanks.very good!
Feb 04 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 viaauto 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.
Feb 04 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 viaauto 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.
Feb 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:Casting unknown bytes to string or char is unsafe, and obviously some bytes can be invalid UTF8 sequences.On Mon, Feb 05, 2018 at 05:48:00AM +0000, FrankLike viaauto 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.
Feb 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.
Feb 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: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/zoH7fEOn Mon, Feb 05, 2018 at 05:48:00AM +0000, FrankLike viaauto 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;
Feb 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:Sorry,'hexString(hash)' is my clerical error. Thank you.I got the answer "no array".auto input = cast(string)hexString(hash);Use toHexString to get the string:
Feb 05 2018