Go Down

Topic: How to get Ascii of character or string ? (Read 4 times) previous topic - next topic

taipscode

Oct 06, 2012, 01:23 am Last Edit: Oct 06, 2012, 06:26 am by taipscode Reason: 1
Hello everybody.

This is a code , char to Ascii
Code: [Select]
#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
Code: [Select]
#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.

dhenry

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


What does that mean?

Tom Carpenter

Code: [Select]
char asciiValueOfOne = '1';
char doCalculations = 'B' + '0' - 'A'
if(doCalculations == asciiValueOfOne){
  //This is true :D
}
~Tom~

fungus


Quote
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?

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

dhenry

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

Tom Carpenter

#5
Oct 06, 2012, 02:14 pm Last Edit: Oct 07, 2012, 04:24 pm by Tom Carpenter Reason: 1
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:
Code: [Select]
 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);
~Tom~

taipscode

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 .

Tom Carpenter

Woops, typo, this:
Code: [Select]
for(byte i = 1; i < sizeof(string) - 1; i++){
Should be this:
Code: [Select]
for(byte i = 0; i < sizeof(string) - 1; i++){
Note the 0 as the starting point so that it includes the first character.
~Tom~

taipscode

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

Go Up