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:

char c2 = stringarray[2][0]; // First 'char' in string #2: '5'
char c1 = stringarray[1][0]; // First 'char' in string #1: 'a'

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;

Reply via email to