Link to home
Start Free TrialLog in
Avatar of Steven Xing
Steven XingFlag for Canada

asked on

How to convert char ASCII to int

For example;
I want to get Int dd = 0 from ASCII byte bb = 48;
Avatar of kaufmed
kaufmed
Flag of United States of America image

I'm sure I don't understand the question, but should it not be:

int dd = (int)bb;

Open in new window

Avatar of ubound
ubound

If you look at the ascii values for the numbers 0-9, you will find that they are all in order, starting at 48.  So, convert the byte to int, then subtract 48.
Avatar of Steven Xing

ASKER

Maybe I didn't explain clearly, I repeat the question in another way:
I read a char, for example '0', from a file and store char as byte 48 in a byte array, then I need to convert 48 to int 0 for calculate.
I don't want to do as int dd = bb-48; I think C# should have some function to do it.
Ah, I see what you mean now. I'm not terribly fond of this (because of the conversion by ToString), but you *can* do:

int dd = Convert.ToInt32(bb.ToString());

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank kaufmed.
NP. Glad to help  = )
"I don't want to do as int dd = bb-48; I think C# should have some function to do it."

From my point of view  - this would be the best solution. It it fast and precise. This is a clever low-level approach.
Using string functions and parse - a bit too long way. Still - it works. If you don't like the first approach.

The last method that kaufmed suggested - is a VERY strange one. I have no idea how it works!
Why it returns 0.25 for a '%' character, e.g.?!

Do like in my code, or read:
http://stackoverflow.com/questions/2866738/whats-the-deal-with-char-getnumericvalue

My output:
0 U+0030 48; d=0
1 U+0031 49; d=1
2 U+0032 50; d=2
3 U+0033 51; d=3
4 U+0034 52; d=4
5 U+0035 53; d=5
6 U+0036 54; d=6
7 U+0037 55; d=7
8 U+0038 56; d=8
9 U+0039 57; d=9
? U+00b2 178; d=2
? U+00b3 179; d=3
? U+00b9 185; d=1
? U+00bc 188; d=0.25
? U+00bd 189; d=0.5
? U+00be 190; d=0.75

class Program
    {
        static void Main(string[] args)
        {

            for (char c = (char)1; c <= 200; c++) //char.MaxValue
            {
                //Console.WriteLine("number = " + char.GetNumericValue(c) + "; char = " + c.);
                double d = char.GetNumericValue(c);
                if (d != -1)
                {
                    Console.WriteLine("{0} U+{1:x4} {2}; d={3}", c, (int)c, (int)c, d);
                }

            }
            Console.ReadLine();
        }
    }

Open in new window

Wow, while I was writing - you chose this stupid method! :)
Exctract 48, my advice!. I don't claim any points :)
Subtract :)
@anarki_jimbel
Why it returns 0.25 for a '%' character, e.g.?!
The method is called GetNumericValue...  why would you call it on something you knew want' numeric!?!?!   ; )

As specified by the docs:

The numeric value of c if that character represents a number; otherwise, -1.0.

Granted that doesn't much explain the return value of 0.25, but based on this statement:

...then I need to convert 48 to int 0 for calculate.

I believe the implication is that some mathematical calculation is going to occur. Unless you know some new-fangled math that uses '%' as a valid value, then I'd say there is nothing wrong with char.GetNumericValue  = )