convert variable to ASCII number

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 28
Reputation: driplet is an unknown quantity at this point 
Solved Threads: 0
driplet driplet is offline Offline
Light Poster

convert variable to ASCII number

 
0
  #1
Dec 19th, 2007
Hi there:

I need to convert a variable of single character to corresponding ASCII number. Can some body tell me how to do this? I tried int(), static_cast(), atoi(), not working.

Thx in advance!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 5
Reputation: Backmat is an unknown quantity at this point 
Solved Threads: 0
Backmat Backmat is offline Offline
Newbie Poster

Re: convert variable to ASCII number

 
0
  #2
Dec 19th, 2007
Here is a website that i found off of google that explains how to do this in a function (Java script) if you just need 1 character changed you can use the converter at the bottom of the page

hope this helps BM
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: convert variable to ASCII number

 
0
  #3
Dec 19th, 2007
>I need to convert a variable of single character to corresponding ASCII number.
First, keep in mind that the world doesn't run on ASCII anymore. However, the char data type is also an integer type, so you can simply treat it as an integer and the problem is solved:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. char foo = 'a';
  6.  
  7. std::cout<< static_cast<int> ( foo ) <<'\n';
  8. }
If that doesn't work for you, I'd recommend posting your current code and explaining exactly what you want rather than making us guess.
Last edited by Narue; Dec 19th, 2007 at 5:51 pm.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: convert variable to ASCII number

 
0
  #4
Dec 19th, 2007
Hi
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. main()
  5. {
  6. char h;
  7.  
  8.  
  9. scanf("%c",&h);
  10. printf("%c= %d",h,h);
  11. system("pause");
  12. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: convert variable to ASCII number

 
0
  #5
Dec 19th, 2007
>#include <stdlib.h>
>#include <stdio.h>
You shouldn't use these headers in C++ because they're deprecated. Your code might work for now, but a future revision of the standard could easily break it.

>main()
This is completely illegal in C++, and it's even illegal in the latest C standard. You're required to explicitly state the return type of a function. Failure to do so even in C89 is considered poor practice.

>system("pause");
This is dangerous and non-portable. To solve the problem portably is tricky, as one of the stickies in this forum describes.

Finally, is there a shortage of spaces where you live? Try intelligently separating tokens with whitespace and watch your code magically become more readable.

Here's a better example for C++:
  1. #include <cstdlib>
  2. #include <cstdio>
  3.  
  4. int main()
  5. {
  6. char h;
  7.  
  8. std::scanf ( "%c", &h );
  9. std::printf ( "%c = %d\n", h, h );
  10. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 290
Reputation: kv79 is an unknown quantity at this point 
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: convert variable to ASCII number

 
0
  #6
Dec 19th, 2007
>#include <stdio.h> and #include <stdlib.h>
So i put in the wrong Thread.OK
>main()
Why shoud i write when i do not need it ,when i need to write then i write.
>return 0;
Same thing .
>system("pause");
Why the ~s.o.s~ did not wrote why is the better getchar();

>white spaces.OK
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 505
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 51
Bench's Avatar
Bench Bench is online now Online
Posting Pro

Re: convert variable to ASCII number

 
0
  #7
Dec 19th, 2007
Originally Posted by elite1986 View Post
>main()
Why shoud i write when i do not need it ,when i need to write then i write.
>return 0;
Same thing .
main returns an int because the standard says it returns an int. if you omit the int return type from main then some compilers will complain and not compile at all, others might allow it to compile but your program will crash, other compilers might do something else.

You don't need return 0; at the end of main in C++ (though it might be good practice to do so), the C++ standard allows an implicit return 0 at the end of main.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 28
Reputation: driplet is an unknown quantity at this point 
Solved Threads: 0
driplet driplet is offline Offline
Light Poster

Re: convert variable to ASCII number

 
0
  #8
Dec 20th, 2007
Thank all of you for taking time on my question.

here is what I did:
input a or more characters from keyboard, then convert it to the corresponding ASCII number.

char u[20];
AnsiString kk;
int G;

kk = Edit1->Text;
strcpy(u, kk.c_str());
G = static_cast<int>(u) ;
[/code]

error message is "can't cast from char* to int".
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,848
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: convert variable to ASCII number

 
0
  #9
Dec 20th, 2007
>>main()
>Why shoud i write when i do not need it ,when i need to write then i write.
You do need to write it. We're on the verge of all of your code breaking because new compilers will refuse to compile it. If you're so lazy that four characters are beyond your comfort zone, you should stop programming immediately.

>>return 0;
>Same thing .
Actually, this isn't the same thing. In C++ and C99, you're forced to explicitly state a return type, which you haven't done. Therefore, I can conclude that you think it's okay not to return a value in C89, which is wrong. Your code exhibits undefined behavior because you've implicitly said main returns an int, but completely failed to return anything. To be more precise, this:
  1. main()
  2. {
  3. }
Is directly equivalent to this:
  1. int main()
  2. {
  3. }
Just because you don't explicitly say main returns int doesn't mean it suddenly doesn't return an int. Your code is wrong, regardless of which C standard you're adhering to, plain and simple.

>>system("pause");
>Why the ~s.o.s~ did not wrote why is the better getchar();
I have no idea what you're trying to say.

>input a or more characters from keyboard, then
>convert it to the corresponding ASCII number.
What do you mean by ASCII number? Do you just want to take "12345" and turn it into the integer value 12345? Your use of the term ASCII doesn't correspond to any rational problem, but it looks like you want this:
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. std::string word;
  8.  
  9. if ( std::cin>> word ) {
  10. // Convert the string to an integer
  11. int value = (int)strtol ( word.c_str(), 0, 0 );
  12.  
  13. std::cout<< value * value <<'\n';
  14. }
  15. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 28
Reputation: driplet is an unknown quantity at this point 
Solved Threads: 0
driplet driplet is offline Offline
Light Poster

Re: convert variable to ASCII number

 
0
  #10
Dec 20th, 2007
Originally Posted by Narue View Post
int main()
{
std::string word;

if ( std::cin>> word ) {
// Convert the string to an integer
int value = (int)strtol ( word.c_str(), 0, 0 );

std::cout<< value * value <<'\n';
}
}
[/code]

sorry made misunderstanding.

What I want to do is, for example, 'A' corresponds '65' in ASCII table, 'B' ---> 66,...'a' ---> 97, so on so forth. not converting a numeric string to a real number, long or short...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC