Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 21, 2006 Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Hi, 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. Thanks! Peter |
August 21, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Thomassen | Peter Thomassen wrote:
> Hi,
>
> 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.
>
> Thanks!
> Peter
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];
}
|
August 21, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to nobody | 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
|
August 22, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Thomassen | 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. |
August 22, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Thomassen | Do you want the numeric (integer/float) value of the string in a char[]? For example, "1.0" => 1.0, and similar? In other words, something similar to: $x = (int) '324'; $y = intval('562'); $z = '324' + '9'; In PHP? If so, you want std.conv. The D code might look like this: x = toInt(324); y = toInt(562); z = toInt(324) + toInt(9); There's also toFloat, toDouble, toUint, etc. See: http://digitalmars.com/d/phobos/std_conv.html Please note that an Exception is thrown if it cannot be converted, it won't just be made 0. So, for example: try x = toInt(user_value); catch (ConvError) x = 0; catch (ConvOverflowError) x = int.max; Or something like that. If that's not what you want, a char[] is actually two numeric values - much like in PHP. It is a length value, and a pointer. Bit shifting these probably won't give you anything interesting. -[Unknown] > Hi, > > 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. > > Thanks! > Peter |
August 23, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Thomassen | On Tue, 22 Aug 2006 01:30:23 +0200, Peter Thomassen <info@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 |
August 23, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote: > On Tue, 22 Aug 2006 01:30:23 +0200, Peter Thomassen <info@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.. I sure did. :-) > "nobody" I love the nick.. have you read the "Deverry" novels by "Katherine Kerr"? > http://www.math.ttu.edu/~kesinger/deverry/kerr.biblio.html > Not yet but I am always looking for good stuff to read. I would guess Nobody comes from Dead Man on some subconscious level: http://www.imdb.com/title/tt0112817/ |
August 23, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to nobody | nobody wrote:
...
>
> 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.
Sure about ulong? In my spare time I made my own minimal Bignum implementation. I'm not sure about shifts, but for addition with carrying it was faster to use size_t (uint in this case) rather than ulong. I wonder if maybe the compiler could optimize it better if it didn't have to emulate 64 bit integers. Or my benchmark was borked.
|
August 23, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chad J | Chad J wrote:
> nobody wrote:
> ....
>>
>> 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.
>
> Sure about ulong? In my spare time I made my own minimal Bignum implementation. I'm not sure about shifts, but for addition with carrying it was faster to use size_t (uint in this case) rather than ulong. I wonder if maybe the compiler could optimize it better if it didn't have to emulate 64 bit integers. Or my benchmark was borked.
I certainly could be wrong but I would be quite surprised. I would be interested to see what tests you ran that suggest addition with carry was faster.
|
August 24, 2006 Re: Numeric access to char[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | <veröffentlicht & per Mail versendet> Hi! Regan Heath schrieb am Mittwoch, 23. August 2006 02:06: > 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); > } Thanks, this is what im looking for! But I don't understand this line: > val = (cast(int*)c.ptr)[0..1][0]; What does [0..1][0] mean? > 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 The oldest example of such pun I know of is Polyphemus who is fooled by this name in Homer's Odyssey which I read parts of in my Latin lessons at school (albeit the original is Greek). --> http://en.wikipedia.org/wiki/Polyphemus Peter |
Copyright © 1999-2021 by the D Language Foundation