User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,218 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,196 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1955 | Replies: 14
Reply
Join Date: Jul 2007
Posts: 28
Reputation: driplet is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
driplet driplet is offline Offline
Light Poster

Question convert variable to ASCII number

  #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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 5
Reputation: Backmat is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Backmat Backmat is offline Offline
Newbie Poster

Re: convert variable to ASCII number

  #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  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: convert variable to ASCII number

  #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 4:51 pm.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Nov 2007
Location: Banjaluka,RS,Bosnia
Posts: 283
Reputation: kv79 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: convert variable to ASCII number

  #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  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: convert variable to ASCII number

  #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. }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Nov 2007
Location: Banjaluka,RS,Bosnia
Posts: 283
Reputation: kv79 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 7
kv79 kv79 is offline Offline
Posting Whiz in Training

Re: convert variable to ASCII number

  #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  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: convert variable to ASCII number

  #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  
Join Date: Jul 2007
Posts: 28
Reputation: driplet is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
driplet driplet is offline Offline
Light Poster

Re: convert variable to ASCII number

  #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  
Join Date: Sep 2004
Posts: 6,333
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 458
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: convert variable to ASCII number

  #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. }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Jul 2007
Posts: 28
Reputation: driplet is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
driplet driplet is offline Offline
Light Poster

Re: convert variable to ASCII number

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC