•
•
•
•
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
![]() |
>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:
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.
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:
cplusplus Syntax (Toggle Plain Text)
#include <iostream> int main() { char foo = 'a'; std::cout<< static_cast<int> ( foo ) <<'\n'; }
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.
•
•
Join Date: Nov 2007
Location: Banjaluka,RS,Bosnia
Posts: 283
Reputation:
Rep Power: 0
Solved Threads: 7
Hi
C Syntax (Toggle Plain Text)
#include <stdlib.h> #include <stdio.h> main() { char h; scanf("%c",&h); printf("%c= %d",h,h); system("pause"); }
>#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++:
>#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++:
cplusplus Syntax (Toggle Plain Text)
#include <cstdlib> #include <cstdio> int main() { char h; std::scanf ( "%c", &h ); std::printf ( "%c = %d\n", h, h ); }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Nov 2007
Location: Banjaluka,RS,Bosnia
Posts: 283
Reputation:
Rep Power: 0
Solved Threads: 7
•
•
•
•
>main()
Why shoud i write when i do not need it ,when i need to write then i write.
>return 0;
Same thing .
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? •
•
Join Date: Jul 2007
Posts: 28
Reputation:
Rep Power: 2
Solved Threads: 0
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".
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".
>>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:
Is directly equivalent to this:
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:
>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:
c Syntax (Toggle Plain Text)
main() { }
c Syntax (Toggle Plain Text)
int main() { }
>>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:
cplusplus Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <string> 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'; } }
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Jul 2007
Posts: 28
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
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...
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Pointers (archived tutorial) (C++)
- How to convert a string to ASCII ? (C++)
- Is there any more Documenation that I need? (C++)
- arithmetic expression using lists (C++)
- shrd instruction and converting integers to ascii (Assembly)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: plz can help me in this q ??
- Next Thread: Linux & C++ compiler



Linear Mode