Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a C++ Expert!

Join 415,729 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,905 people online right now.Registration is fast and FREE... Join Now!



convert char to int and int to char Rate Topic: -----

#1 tryingcpp  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-September 07


Dream Kudos: 0

Share |

convert char to int and int to char

Post icon  Posted 17 September 2007 - 01:31 AM

Hi! Please help me to convert a char to int and int to char on Dev C++. I tried with (int)value and int(value) but it doesnt work. Thank you!
Was This Post Helpful? 0
  • +
  • -


#2 spijo  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-September 07


Dream Kudos: 0

Re: convert char to int and int to char

Posted 17 September 2007 - 02:01 AM

View Posttryingcpp, on 17 Sep, 2007 - 02:31 AM, said:

Hi! Please help me to convert a char to int and int to char on Dev C++. I tried with (int)value and int(value) but it doesnt work. Thank you!



if lets say u wanna convert the char a...(int)' a' will return the ascii value of a and (char)97 will return yo a if am not forgetting something
Was This Post Helpful? 0
  • +
  • -

#3 help-linux  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 3
  • View blog
  • Posts: 54
  • Joined: 12-August 06


Dream Kudos: 50

Re: convert char to int and int to char

Posted 17 September 2007 - 03:34 AM

If you are trying to convert the string itself to an int type, you can use the C function, atoi().

const char * intStr = "10";
int myint = atoi(intStr);



If you are converting an int to a string use the C function sprintf

char Str[16];
int myint = 15;
sprintf(Str, "%d", myint);

Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • Icon

Reputation: 1035
  • View blog
  • Posts: 7,734
  • Joined: 18-February 07


Dream Kudos: 1250

Expert In: Java/C++

Re: convert char to int and int to char

Posted 17 September 2007 - 12:19 PM

if you are coverting a character like '9' to an integer then simply think about ascii table.

char c = '5';
int valOfC = (int)(c-'0'); //'5'= 53, '0' = 48, '5' - '0' = 53 - 48 = 5

Was This Post Helpful? 1



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users