Numeric access to char[]
nobody
nobody at mailinator.com
Mon Aug 21 18:01:11 PDT 2006
Peter Thomassen wrote:
> nobody schrieb am Dienstag, 22. August 2006 00:54:
>>> how is it possible to work on the numeric value of a char[]? I'm
>>> interested in bit shifting and arithmetic operations on the numeric
>>> value.
>> I am pretty sure you can just treat a char as a ubyte. The char type is 8
>> bits and unsigned. However if it makes it easier for you then you might
>> try this:
>>
>> int main(char[][] args)
>> {
>> ubyte[] num1 = cast(ubyte) args[0];
>> }
>
> When casting to ubyte[], this works fine. But I actually meant the numeric
> value of char[], not the one of char. Do I need to construct it from the
> single chars, or can I, for example, right-shift a whole char[] by 1?
>
> Peter
Sorry I misunderstood you! I am still not completely sure what you are after but
I think it sounds likely you mean this:
char[] ex = "azAZ";
// 'a' = 65 = 01000001 (binary)
// 'z' = 90 = 01011010 (binary)
// 'A' = 97 = 01100001 (binary)
// 'Z' = 122= 01111010 (binary)
01000001 01011010 01100001 01111010
becomes (carries from one pos to the next)
00100000 10101101 00110000 10111101
instead of (right 1s fall off)
00100000 00101101 00110000 00111101
I am pretty sure the only likely candidate for that would have been std.bitarray
but the docs don't include the shift operators:
http://digitalmars.com/d/phobos/std_bitarray.html
So you will have to do it manually. I would like to suggest that if you can pad
the char[] to ensure its .length % 8 == 0 then you can cast it to a ulong and
your shifting will be faster.
More information about the Digitalmars-d-learn
mailing list