char ----> int


S

sahel

Hi;
I wrote a program that I want to put a number in char (I mean : char
s = '8';)
7 than put the number I put that at char s to int ;
But it does not work correctly ;
I mean it doesn’t show 8 ;
using System;
class Program
{
static void Main(string[] args)
{
char s = '8';
int f =s;
Console.WriteLine(f);
}
}

thanks . . .
 
Ad

Advertisements

T

Tony Johansson

"sahel" <[email protected]> skrev i meddelandet
Hi;
I wrote a program that I want to put a number in char (I mean : char
s = '8';)
7 than put the number I put that at char s to int ;
But it does not work correctly ;
I mean it doesn't show 8 ;
using System;
class Program
{
static void Main(string[] args)
{
char s = '8';
int f =s;
Console.WriteLine(f);
}
}

thanks . . .

All writeable characters has a specific representation in ASCII for example
the chracter '8' is represented by
number 56. When you do
int f =s;
you say tell me what number character '8' represents and you will have
number 56

//Tony
 
S

sahel

"sahel" <[email protected]> skrev i meddelandetHi;
I wrote a program that I want to put  a number in char (I mean : char
s = '8';)
7 than put the number I put that at  char s to int ;
But it does not work correctly ;
I mean it doesn't show 8 ;
 using System;
    class Program
    {
        static void Main(string[] args)
        {
            char s = '8';
            int f =s;
            Console.WriteLine(f);
        }
    }

thanks . . .

All writeable characters has a specific representation in ASCII for example
the chracter '8' is represented by
number 56. When you do
int f =s;
you say tell me what number character '8' represents and you will have
number 56

//Tony

hi;
i want this answer because i want to get some char from user like
this : y=(x^2)+1 so i will put 2 in char but to write the program for
it that it must do an math question ,program must know that 2 & 1 are
not char they are int
 
K

Konrad Neitzel

Hi Sahel!

sahel said:
Hi;
I wrote a program that I want to put a number in char (I mean : char
s = '8';)
7 than put the number I put that at char s to int ;
But it does not work correctly ;
I mean it doesn’t show 8 ;
using System;
class Program
{
static void Main(string[] args)
{
char s = '8';
int f =s;
Console.WriteLine(f);
}
}

thanks . . .

You already got answers that told you a little about the different types.
Maybe you simply read more about ascii and UTF(-8/-16/-32). Good sources can
be found through google or simply check the wikipedia.

The .Net Framework provides functions to get information from one type to
another. In case of reading stings (Which are multiple characters), the
functions you would like are:
Int32.Parse and Int32.TryParse.

Just check msdn for a description of these functions (and msdn often has an
example that shows the possibilities in a quick way!).

Mentioned Links:
http://en.wikipedia.org/wiki/Main_Page
http://msdn.microsoft.com/en-us/library/default.aspx

With kind regards,

Konrad
 
J

Jeff Johnson

i want this answer because i want to get some char from user like
this : y=(x^2)+1 so i will put 2 in char but to write the program for
it that it must do an math question ,program must know that 2 & 1 are
not char they are int

Subtract 48 from each digit that you convert from char to int and you'll
have what you want. Of course, that's the long way. There's also the Convert
class or the int.Parse() / int.TryParse() methods.
 
A

Andrew Poelstra

Subtract 48 from each digit that you convert from char to int and you'll
have what you want. Of course, that's the long way. There's also the Convert
class or the int.Parse() / int.TryParse() methods.

Or subtract '0' and you'll not only be clearer, your code will
work outside of ASCII-based encodings.
 
Ad

Advertisements

A

Arne Vajhøj

Or subtract '0' and you'll not only be clearer, your code will
work outside of ASCII-based encodings.

It will work with encoding where the digits are in order.

But EBCDIC is not that common in .NET programs.

But the readability argument is still valid.

Arne
 
A

Arne Vajhøj

i want this answer because i want to get some char from user like
this : y=(x^2)+1 so i will put 2 in char but to write the program for
it that it must do an math question ,program must know that 2& 1 are
not char they are int

If you want to read and interpret an expression in a string, then
note that it is more complex than simple char-int conversions.

Arne
 
G

Göran Andersson

Arne said:
It will work with encoding where the digits are in order.

But EBCDIC is not that common in .NET programs.

But the readability argument is still valid.

Arne

A char always contain a Unicode character, it's not encoded.
 
G

Göran Andersson

Göran Andersson said:
A char always contain a Unicode character, it's not encoded.

(Well, it's kind of encoded as strings are represented internally as
UTF-16/UCS-2 values rather than full 32 bit unicode code points, but
that's a different matter that's not really relevant here.)
 
P

Peter Duniho

Göran Andersson said:
(Well, it's kind of encoded as strings are represented internally as
UTF-16/UCS-2 values rather than full 32 bit unicode code points, but
that's a different matter that's not really relevant here.)

I think it is relevant. Strings and characters in .NET are documented
to be UTF-16. It's not like it's some "internal-only implementation
detail"; it's an integral part of the design of .NET, just as the
numerical formats for Int32, Double, etc. are. That is, a program can
safely rely on the specific format to accomplish things, without
worrying that it might change in the future.

Pete
 
Ad

Advertisements

Ad

Advertisements

A

Arne Vajhøj

I think it is relevant. Strings and characters in .NET are documented to
be UTF-16. It's not like it's some "internal-only implementation
detail"; it's an integral part of the design of .NET, just as the
numerical formats for Int32, Double, etc. are. That is, a program can
safely rely on the specific format to accomplish things, without
worrying that it might change in the future.

I think he meant that the surrogate pair problem was not
relevant for the '0'..'9' discussion.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top