Converting string to ascii value

Ali Çehreli acehreli at yahoo.com
Fri Mar 7 14:33:05 PST 2014


On 03/07/2014 02:21 PM, Setra wrote:

 > On Friday, 7 March 2014 at 22:21:06 UTC, Setra wrote:
 >> Hello all! I am having trouble converting a letter in a array of
 >> string to the ascii value. For example:
 >>
 >> string[] stringarray[3];

That is three string slices, not three strings. This works for the 
following statements:

     string[3] stringarray;

 >> stringarray[0] = "blahblahblah";
 >> stringarray[1] = "a";
 >> stringarray[2] = "5";
 >>
 >> long y = to!long(stringarray[2]); // makes y the value 5
 >> long x = to!long(stringarray[1]); // errors
 >>
 >>
 >> This is not working properly. I want to get the ascii value of the
 >> characters.
 >> In this case y should equal 97, b should equal 53. How do I do this
 >> properly?
 >> Thanks!

You don't need any conversion. Characters are encoding values anyway:

     auto s = "a";
     assert(s[0] == 97);

However, don't forget that strings in D are UTF-encoded. So, what you 
expect will work only for ASCII content.

Ali



More information about the Digitalmars-d-learn mailing list