www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Return ASCII value as uint

reply okibi <okibi ratedo.com> writes:
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
next sibling parent Johan Granberg <lijat.meREM OVEgmail.com> writes:
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
prev sibling parent reply Alexander Panek <alexander.panek brainsware.org> writes:
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
parent reply okibi <okibi ratedo.com> writes:
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
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
parent reply John Ohno <john.ohno gmail.com> writes:
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
next sibling parent reply okibi <okibi ratedo.com> writes:
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
parent reply Jari-Matti =?ISO-8859-1?Q?M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
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
parent okibi <okibi ratedo.com> writes:
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 08 2007
prev sibling parent reply "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"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.
May 10 2007
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Stewart Gordon wrote:
 "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.

<con> It makes someone have to go lookup what toUint actually does. Rather than being able to tell immediately by virtue of knowing basic D syntax. <pro> toUint(3+foo(x)) is a little easier on the eyes than cast(uint)(3+foo(x)). --bb
May 10 2007