digitalmars.D.learn - Converting string to ascii value
- Setra (13/13) Mar 07 2014 Hello all! I am having trouble converting a letter in a array of
- Setra (2/15) Mar 07 2014 Whoops this is the correct version.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/28) Mar 07 2014 That is three string slices, not three strings. This works for the
- Setra (2/2) Mar 07 2014 Thanks! Now I feel kind of dumb.
- Nick Sabalausky (2/4) Mar 07 2014 In a lot of languages it isn't that simple ;)
- H. S. Teoh (8/25) Mar 07 2014 [...]
- Nick Sabalausky (3/4) Mar 07 2014 long x = cast(ubyte) stringarray[0][0];
- Nick Sabalausky (33/46) Mar 07 2014 This isn't your main problem, but that line is incorrect. Actually, I'm
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
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
On 03/07/2014 02:21 PM, Setra wrote:On Friday, 7 March 2014 at 22:21:06 UTC, Setra wrote:That is three string slices, not three strings. This works for the following statements: string[3] stringarray;Hello all! I am having trouble converting a letter in a array of string to the ascii value. For example: string[] stringarray[3];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. Alistringarray[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!
Mar 07 2014
Thanks! Now I feel kind of dumb. For some reason I thought it would not be that simple...
Mar 07 2014
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
On Fri, Mar 07, 2014 at 10:21:57PM +0000, Setra wrote:On Friday, 7 March 2014 at 22:21:06 UTC, Setra wrote:[...] 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...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?
Mar 07 2014
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
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