Pages: [1]   Go Down
Author Topic: How to get Ascii of character or string ?  (Read 4511 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 Hello everybody.

This is a code , char to Ascii
Code:
#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:
#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.
« Last Edit: October 05, 2012, 11:26:14 pm by taipscode » Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

What does that mean?
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1730
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

~Tom~

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 150
Posts: 5744
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

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

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1730
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 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);
« Last Edit: October 07, 2012, 09:24:57 am by Tom Carpenter » Logged

~Tom~

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1730
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

~Tom~

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: