www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert a hex string into a ubyte[] or OutBuffer

reply "Darren" <darrenhobbs mac.com> writes:
Hi,

I'm trying to do something very basic with large numbers:

Let's say I have a hex representation of a large number:

String hexnum = "16D81B16E091F31BEF";

I'd like to convert it into a ubyte[] in order to Base64 encode 
it (or, indeed ASCII85 or Base32).

eg, [16, D8, 1B, 16, E0, 91, F3, 1B, EF]

Is there an idiomatic/simple way to do that?

For example, node's Buffer class supports the following API:

var buffer = new Buffer(hexnum, "hex");

It also supports "base64" and "utf8".

This seems like a very nice way to get string representations of 
binary data into a buffer :) Is there a D equivalent?

I was looking at how the uuid module does it, I can use that as a 
roadmap, I just wanted to check if there was a shorter way 
already present in the libs.

Many thanks,
-Darren
May 19 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Darren:

 Let's say I have a hex representation of a large number:

 String hexnum = "16D81B16E091F31BEF";

 I'd like to convert it into a ubyte[]
A simple way is to use hex strings and then cast it to immutable(ubyte)[]: void main() { immutable hexNum = cast(immutable(ubyte)[])x"16D81B16E091F31BEF"; } The immutable at the cast point is needed because string literals are immutable in D. If you need a mutable ubyte[] you need a dup. Also vote here: https://issues.dlang.org/show_bug.cgi?id=5909 Bye, bearophile
May 19 2014
prev sibling parent reply "anonymous" <anonymous example.com> writes:
On Monday, 19 May 2014 at 11:36:43 UTC, Darren wrote:
 String hexnum = "16D81B16E091F31BEF";
string (lowercase)
 I'd like to convert it into a ubyte[] in order to Base64 encode 
 it (or, indeed ASCII85 or Base32).

 eg, [16, D8, 1B, 16, E0, 91, F3, 1B, EF]

 Is there an idiomatic/simple way to do that?
import std.conv: parse; import std.array: array; import std.range: chunks; import std.algorithm: map; ubyte[] bytes = hexnum /* "16D8..." */ .chunks(2) /* "16", "D8", ... */ .map!(twoDigits => twoDigits.parse!ubyte(16)) /* 0x16, 0xD8, ... */ .array();
May 19 2014
parent reply "Darren" <darrenhobbs mac.com> writes:
On Monday, 19 May 2014 at 12:28:14 UTC, anonymous wrote:
 On Monday, 19 May 2014 at 11:36:43 UTC, Darren wrote:
 Is there an idiomatic/simple way to do that?
import std.conv: parse; import std.array: array; import std.range: chunks; import std.algorithm: map; ubyte[] bytes = hexnum /* "16D8..." */ .chunks(2) /* "16", "D8", ... */ .map!(twoDigits => twoDigits.parse!ubyte(16)) /* 0x16, 0xD8, ... */ .array();
Nice, thanks! I've added a slightly edited version as the answer to this question on stackoverflow: http://stackoverflow.com/a/23741556/47481
May 19 2014
parent reply zabruk70 <sorry noem.ail> writes:
Hello.
In modern phobos ver 2.069.1 exists template hexString

https://dlang.org/phobos/std_conv.html#.hexString

to convert hex string to bytes.
It works in compile time only.
But what if i need it in run time?

Is the answer in this topic still best way?
Or now we have some function/template in phobos?

Thanks.
Jan 07
next sibling parent Basile B. <b2.temp gmx.com> writes:
On Thursday, 7 January 2016 at 21:00:06 UTC, zabruk70 wrote:
 Hello.
 In modern phobos ver 2.069.1 exists template hexString

 https://dlang.org/phobos/std_conv.html#.hexString

 to convert hex string to bytes.
 It works in compile time only.
 But what if i need it in run time?

 Is the answer in this topic still best way?
 Or now we have some function/template in phobos?

 Thanks.
The original PR that proposed a template to translate x strings as a template contained the Run-Time version too. Archeology... https://github.com/D-Programming-Language/phobos/pull/3058/files#diff-ebd0b0e1b0171283328bda4a570616b9R5570 But otherwise take the solution by anonymous. The thing with hexString is just that it handles ascii whites so it can be used to process hex dumps directly using the import expression: --- static ubyte[] dumpToArray = hexString!import(dump.txt); --- Which was maybe a "faddishness" from Walter (no offense here, but it's clear that D is a bit over the top as for the profusion of string literals: x"", r"", ``, q{}, ""w, ""d, delimited, here doc, ...). :)
Jan 07
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Thursday, 7 January 2016 at 21:00:06 UTC, zabruk70 wrote:
 Hello.
 In modern phobos ver 2.069.1 exists template hexString

 https://dlang.org/phobos/std_conv.html#.hexString

 to convert hex string to bytes.
 It works in compile time only.
 But what if i need it in run time?

 Is the answer in this topic still best way?
 Or now we have some function/template in phobos?

 Thanks.
Damn, I've been trapped, thread exhumated from 2014 ...
Jan 07
parent zabruk70 <sorry noem.ail> writes:
On Thursday, 7 January 2016 at 23:15:41 UTC, Basile B. wrote:
 Damn, I've been trapped, thread exhumated from 2014 ...
Yes, but my post was made yesterday. I don't want create new post and found this. Thank you for your time.
Jan 08