Re: Convert a hex string into a ubyte[] or OutBuffer

2016-01-08 Thread zabruk70 via Digitalmars-d-learn

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.


Re: Convert a hex string into a ubyte[] or OutBuffer

2016-01-07 Thread Basile B. via Digitalmars-d-learn

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 ...


Re: Convert a hex string into a ubyte[] or OutBuffer

2016-01-07 Thread Basile B. via Digitalmars-d-learn

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, ...).


:)




Re: Convert a hex string into a ubyte[] or OutBuffer

2016-01-07 Thread zabruk70 via Digitalmars-d-learn

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.


Convert a hex string into a ubyte[] or OutBuffer

2014-05-19 Thread Darren via Digitalmars-d-learn

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


Re: Convert a hex string into a ubyte[] or OutBuffer

2014-05-19 Thread bearophile via Digitalmars-d-learn

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)[])x16D81B16E091F31BEF;

}

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


Re: Convert a hex string into a ubyte[] or OutBuffer

2014-05-19 Thread anonymous via Digitalmars-d-learn

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();


Re: Convert a hex string into a ubyte[] or OutBuffer

2014-05-19 Thread Darren via Digitalmars-d-learn

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