Jump to page: 1 2
Thread overview
Return ASCII value as uint
May 08, 2007
okibi
May 08, 2007
Johan Granberg
May 08, 2007
Alexander Panek
May 08, 2007
okibi
May 08, 2007
John Ohno
May 08, 2007
okibi
May 08, 2007
okibi
May 10, 2007
Stewart Gordon
May 10, 2007
Bill Baxter
May 08, 2007
Hey,

I was wondering if D has a simple function to return the ASCII value of a character as a uint, much like ord() does in php.

Thanks!
May 08, 2007
okibi wrote:

> Hey,
> 
> I was wondering if D has a simple function to return the ASCII value of a character as a uint, much like ord() does in php.
> 
> Thanks!

You could just assign the charachter to a uint variabel, that should work.
May 08, 2007
On Tue, 08 May 2007 11:27:40 -0400
okibi <okibi@ratedo.com> wrote:

> Hey,
> 
> I was wondering if D has a simple function to return the ASCII value of a character as a uint, much like ord() does in php.
> 
> Thanks!

Shouldn't a cast be sufficiant?

char c = 'a';
uint i = cast(uint)c;
May 08, 2007
Alexander Panek Wrote:

> On Tue, 08 May 2007 11:27:40 -0400
> okibi <okibi@ratedo.com> wrote:
> 
> > Hey,
> > 
> > I was wondering if D has a simple function to return the ASCII value of a character as a uint, much like ord() does in php.
> > 
> > Thanks!
> 
> Shouldn't a cast be sufficiant?
> 
> char c = 'a';
> uint i = cast(uint)c;

Yes, a cast would work, however I was wondering if there was an actual function that I could use to grab the value while passing it to another function, specifically this function:

saveItem.addAccelerator("activate",accelGroup,i,GdkModifierType.CONTROL_MASK,accelFlags);

That function contains the i variable from your function. Is there a simple function, or should I use a cast?
May 08, 2007
"okibi" <okibi@ratedo.com> wrote in message news:f1q8ne$3147$1@digitalmars.com...
> That function contains the i variable from your function. Is there a simple function, or should I use a cast?

Just cast it.  chars are just integers; you can freely cast between characters and integers.


May 08, 2007
Jarrett Billingsley Wrote:

> "okibi" <okibi@ratedo.com> wrote in message news:f1q8ne$3147$1@digitalmars.com...
> > That function contains the i variable from your function. Is there a simple function, or should I use a cast?
> 
> Just cast it.  chars are just integers; you can freely cast between characters and integers.
> 
> 

A good general template function (will work for anything that can be explicity casted to uint, including chars, doubles, and pointers):

uint toUint!(T)(T c) {
        return (cast(uint)c);
}
May 08, 2007
John Ohno Wrote:

> Jarrett Billingsley Wrote:
> 
> > "okibi" <okibi@ratedo.com> wrote in message news:f1q8ne$3147$1@digitalmars.com...
> > > That function contains the i variable from your function. Is there a simple function, or should I use a cast?
> > 
> > Just cast it.  chars are just integers; you can freely cast between characters and integers.
> > 
> > 
> 
> A good general template function (will work for anything that can be explicity casted to uint, including chars, doubles, and pointers):
> 
> uint toUint!(T)(T c) {
>         return (cast(uint)c);
> }

Well, that doesn't compile. Is the ! needed? Anyways, if I pass an "s" to that, it should return the ASCII value (115 I think), right?

May 08, 2007
okibi wrote:
> John Ohno Wrote:
>> uint toUint!(T)(T c) {
>>         return (cast(uint)c);
>> }
> 
> Well, that doesn't compile. Is the ! needed? Anyways, if I pass an "s" to that, it should return the ASCII value (115 I think), right?

That ! is a typo.
May 08, 2007
Jari-Matti Mäkelä Wrote:

> okibi wrote:
> > John Ohno Wrote:
> >> uint toUint!(T)(T c) {
> >>         return (cast(uint)c);
> >> }
> > 
> > Well, that doesn't compile. Is the ! needed? Anyways, if I pass an "s" to that, it should return the ASCII value (115 I think), right?
> 
> That ! is a typo.

I thought it was, works great without it!

Thanks,
Okibi
May 10, 2007
"John Ohno" <john.ohno@gmail.com> wrote in message news:f1q9n0$1bd$1@digitalmars.com...
<snip>
> A good general template function (will work for anything that can be explicity casted to uint, including chars, doubles, and pointers):
>
> uint toUint!(T)(T c) {
>        return (cast(uint)c);
> }

Is there really any point using this when you can just cast when you need to?  OK, so it saves two characters where it's used....

Stewart. 

« First   ‹ Prev
1 2