Numeric access to char[]

Regan Heath regan at
Tue Aug 22 17:06:51 PDT 2006

On Tue, 22 Aug 2006 01:30:23 +0200, Peter Thomassen  
<info at> 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?

Depends what exactly you're trying to do, perhaps this:

import std.stdio;

void main()
	char[] c = "azAZ";
	int val;	
	val = (cast(int*)c.ptr)[0..1][0];
	val >>= 1;


p.s. nobody got the ascii values backward ('A' is 65, 'a' is 97)
it's nobody's fault really.. nobody is to blame..

"nobody" I love the nick.. have you read the "Deverry" novels by  
"Katherine Kerr"?

More information about the Digitalmars-d-learn mailing list