www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting string to ascii value

reply "Setra" <roederharpo hushmail.com> writes:
Hello all! I am having trouble converting a letter in a array of 
string to the ascii value. For example:

string[] stringarray[3];
stringarray[0] = "blahblahblah";
stringarray[1] = "a";
stringarray[3] = "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!
Mar 07 2014
next sibling parent reply "Setra" <roederharpo hushmail.com> writes:
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];
 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!
Whoops this is the correct version.
Mar 07 2014
next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Mar 07 2014
parent reply "Setra" <roederharpo hushmail.com> writes:
Thanks! Now I feel kind of dumb.
For some reason I thought it would not be that simple...
Mar 07 2014
parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On 3/7/2014 5:37 PM, Setra wrote:
 Thanks! Now I feel kind of dumb.
 For some reason I thought it would not be that simple...
In a lot of languages it isn't that simple ;)
Mar 07 2014
prev sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Mar 07, 2014 at 10:21:57PM +0000, 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];
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?
[...] long x = cast(ubyte) stringarray[0]; Using to!long is for when you're trying to parse a number represented in string format. T -- Famous last words: I *think* this will work...
Mar 07 2014
parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On 3/7/2014 5:33 PM, H. S. Teoh wrote:
 long x = cast(ubyte) stringarray[0];
long x = cast(ubyte) stringarray[0][0]; ;)
Mar 07 2014
prev sibling parent Nick Sabalausky <SeeWebsiteToContactMe semitwist.com> writes:
On 3/7/2014 5:21 PM, Setra wrote:
 Hello all! I am having trouble converting a letter in a array of string
 to the ascii value. For example:
First of all:
 string[] stringarray[3];
This isn't your main problem, but that line is incorrect. Actually, I'm kinda surprised that even works. It should be one of these, depending if you want a static array or a dynamic one: string[3] staticStringArray; string[] dynamicStringArray; dynamicStringArray.length = 3; Or you can do it all in one like this: string[] dynamicStringArray = ["blahblahblah", "a", "5"];
 stringarray[0] = "blahblahblah";
 stringarray[1] = "a";
 stringarray[3] = "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!
First of all, ASCII is outdated, it's all Unicode now. D's strings are UTF-8. See this if you need a primer on Unicode: http://www.joelonsoftware.com/articles/Unicode.html That said, the English letters and numeric digits just happen to be the same in UTF-8 as ASCII, so in your examples, you can still get the ASCII values (as long as you remember it's *really* Unicode's UTF-8). Just keep in mind you don't normally want to be dealing with ASCII or even individual characters. Usually you want to just stick with with full strings. Back to your code though, your code is trying to convert the *entire* string to a long. So that's not going to get you want you want. You want the numeric representation of an *individual* character *within* the string. In your example, you'd do that like this: Again, remember those aren't really characters, they're UTF-8 "code units". So if your strings have any non-english characters, then that won't always work as you expect. Now, to get the numeric representation of c1 and c2 (ie the UTF-8 code unit, which in your example just happens to also be the ASCII code, but only by pure chance), you can just cast it to a ubyte: ubyte y = cast(ubyte)c2; ubyte x = cast(ubyte)c1;
Mar 07 2014