FormattedRead hex string
Jason Spencer
spencer8 at sbcglobal.net
Mon Sep 24 15:39:51 PDT 2012
On Monday, 24 September 2012 at 16:32:45 UTC, monarch_dodra wrote:
> On Monday, 24 September 2012 at 15:05:54 UTC, Jason Spencer
> wrote:
>> I imagine there's a slick way to do this, but I'm not seeing
>> it.
>>
>> I have a string of hex digits which I'd like to convert to an
>> array of 8 ubytes:
>>
>> 0123456789abcdef --> [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB,
>> 0xCD, 0xEF]
<snip>
>> void main(string[] args)
>> {
>> ubyte[8] b;
>>
>> formattedRead(args[1], "%(%s%)", &b);
>> }
> I think that you are not supposed to use a static array: If
> there are not EXACTLY as many array elements as there are
> parse-able elements, then the formatted read will consider the
> parse to have failed.
The sample code was just for testing convenience. In practice
the string will be conditioned and known to have 16 characters in
{0-9, a-f}.
>
> Try this, it's what you want, right?
>
> --------
> void main()
> {
> string s = "ffff fff ff f";
> ushort[] vals;
> formattedRead(s, "%(%x %)", &vals);
> writefln("%(%s - %)", vals);
> }
Not quite. You've taken the liberty of using a
delimiter--spaces. I have to take 16 contiguous, NON-delimited
hex digits and produce 8 bytes. So I could read it as a uint64
(not uint16, as I mistakenly posted before), but then I'd have to
byte-reverse it. I could use slicing and do a byte at a time. I
just wondered if there were a slick way to get in-place data from
a contiguous hex string.
Thanks,
Jason
More information about the Digitalmars-d-learn
mailing list