| | |
convert variable to ASCII number
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>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:
C++ 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 5:51 pm.
New members chased away this month: 4
•
•
Join Date: Nov 2007
Posts: 290
Reputation:
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++:
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <cstdio> int main() { char h; std::scanf ( "%c", &h ); std::printf ( "%c = %d\n", h, h ); }
New members chased away this month: 4
•
•
•
•
>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:
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:
C++ 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'; } }
New members chased away this month: 4
•
•
Join Date: Jul 2007
Posts: 28
Reputation:
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...
![]() |
Similar Threads
- How to convert a string to ASCII ? (C++)
- Pointers (archived tutorial) (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
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






