Numeric access to char[]

Regan Heath regan at netwin.co.nz
Tue Aug 22 17:06:51 PDT 2006


On Tue, 22 Aug 2006 01:30:23 +0200, Peter Thomassen  
<info at peter-thomassen.de> 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];
	//DEBUG
	//writef("(%02d)%08b",c[0],c[0]);
	//writef(",(%02d)%08b",c[1],c[1]);
	//writef(",(%02d)%08b",c[2],c[2]);
	//writefln(",(%02d)%08b",c[3],c[3]);
	writefln("%032b",val);
	val >>= 1;
	writefln("%032b",val);
}

Regan

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"?
http://www.math.ttu.edu/~kesinger/deverry/kerr.biblio.html




More information about the Digitalmars-d-learn mailing list