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!

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

>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:

#include <iostream>

int main()
{
  char foo = 'a';

  std::cout<< static_cast<int> ( foo ) <<'\n';
}

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.

Hi

#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 <cstdlib>
#include <cstdio>

int main()
{
  char h;

  std::scanf ( "%c", &h );
  std::printf ( "%c = %d\n", h, h );
}

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

>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.

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) ;

error message is "can't cast from char* to int".

Edited 3 Years Ago by Dani: Formatting fixed

>>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:

main()
{
}

Is directly equivalent to this:

int main()
{
}

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:

#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';
  }
}
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';
  }
}

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...

Edited 3 Years Ago by diafol: fixed formatting

>What I want to do is, for example, 'A' corresponds '65' in ASCII table
So what's stopping you from doing what was suggested originally? Loop through the string and print each character's integer value.

I just need to convert the fist character, instead of the whole character string. So I don't have to use a loop, right? If I simply put a string constant like static_cast<int>('A'), it is no problem. But when I define char A, and input a character to A, then use static_cast<int>(A) to convert it, the result is not right.

>But when I define char A, and input a character to A, then
>use static_cast<int>(A) to convert it, the result is not right.
Then you're not doing it right:

#include <iostream>
#include <string>

int main()
{
  std::string line;

  if ( getline ( std::cin, line ) ) {
    char first = line[0];

    std::cout<<"First char: "<< (int)line[0] <<'\n';
    std::cout<<"Separate char: "<< (int)first <<'\n';

    std::cout<<"All of them:\n";

    for ( std::string::size_type i = 0; i < line.size(); i++ )
      std::cout<< line[i] <<" = "<< (int)line[i] <<'\n';
  }
}
#include <iostream>
#include <string>

int main()
{
  std::string line;

  if ( getline ( std::cin, line ) ) {
    char first = line[0];

    std::cout<<"First char: "<< (int)line[0] <<'\n';
    std::cout<<"Separate char: "<< (int)first <<'\n';

    std::cout<<"All of them:\n";

    for ( std::string::size_type i = 0; i < line.size(); i++ )
      std::cout<< line[i] <<" = "<< (int)line[i] <<'\n';
  }
}

Thank you for the code.

I could compile it in both BCB and VCC but encounter error when ran the code in BCB:
"Access violation at address xxxx..."
Have no idea what caused this.

>Have no idea what caused this.
When you ran the program you probably just typed Enter. The program assumes the string isn't empty.

This article has been dead for over six months. Start a new discussion instead.