How to get Ascii of character or string ?

Hello everybody.

This is a code , char to Ascii

#include <stdio.h>
#include <conio.h>

main()
{
char c;
int a;
printf("Enter charater:",c);
scanf("%c",&c);

printf("%d",c);
getch();

}

Example :
Enter a character : a
Result : 97

And this is a code ,string to several Ascii

 #include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
char *str1;
char st[80];
do
{
str1=st;
cin.get(st,80);
while(*str1)
cout<<int(*str1++)<<" ";
}
while(strcmp(st,"done")==0);

getch();
}

Example : Enter a string
Hello abcd
Result : 72 101 108 111 32 97 98 99 100

These codes run good with Visual C++6.0 but not run in Arduino ,
can you help me modify them.
Thanks a lots.

Can you help me get Ascii number of character or string ?

What does that mean?

char asciiValueOfOne = '1';
char doCalculations = 'B' + '0' - 'A'
if(doCalculations == asciiValueOfOne){
  //This is true :D
}

dhenry:

Can you help me get Ascii number of character or string ?

What does that mean?

And what does it have to do with LEDs and/or multiplexing?

These codes run good with Visual C++6.0 but not run in Arduino ,
can you help me modify them.

Two options:

  1. Get the source code for Visual C++6.0 and port it to your Arduino. If it requires Java, you will have to ask Sun as well. With that, you can recompile your code and it will run on Arduino; or
  2. Understand what Arduino is and isn't and write your code for Arduino.

You can figure out for yourself which option is best for you.

There are probably better ways, but this is one example of how to convert a string of ascii characters to a string of their decimal equivalent:

  char string[] = "This is a string of ASCII Characters";
  char buffer[4*sizeof(string)]; //sized for the worst case scenario of each being in the hundreds plus a space between each and a null
  char* buffPtr = buffer;
  
  for(byte i = 0; i < sizeof(string) - 1; i++){
    itoa((int)string[i],buffPtr,10); //convert the next character to a string and store it in the buffer
    buffPtr += strlen(buffPtr); //move on to the position of the null character
    *buffPtr = ' '; //replace with a space
    buffPtr++; //move on ready for next
  }
  buffPtr--; //move back a character to where the final space (' ') is
  *buffPtr = '\0'; //replace it with a null to terminate the string
  Serial.println(buffer);

Hi Tom
I just tested your code ,it's good but no display the first character of string.
Examle : char string[] = "abcd";
Result : 98 99 100
not display 97 of "a"
Can you modify ?
Thanks .

Woops, typo, this:

for(byte i = 1; i < sizeof(string) - 1; i++){

Should be this:

for(byte i = 0; i < sizeof(string) - 1; i++){

Note the 0 as the starting point so that it includes the first character.

Yeah. It's OK.
Thankyou very much ,can you give me your email , nick Yahoo or Skype ? Because you make me very happy :slight_smile: