View previous topic :: View next topic |
Author |
Message |
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
convert ascii value to character |
Posted: Sun Mar 22, 2009 6:21 pm |
|
|
Hi all!
Is there a way to convert ascii value to character?
For example I have the int8 number 65, I want to get back the character "A" in char format. something like the "toascii" which is not included in CCS.
Regards |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sun Mar 22, 2009 6:33 pm |
|
|
Just cast the int8 to type char. At the machine level nothing actually happens as both representations are the same in binary.
If you putc(65) you will get an "A" out the serial port. Putc() just looks at the binary and doesn't know what data type it is. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 22, 2009 6:38 pm |
|
|
Quote: | something like the "toascii" which is not included in CCS |
It's normally in ctype.h, and it's a just a simple macro:
Code: | #define toascii(c) ((c) & 0x7f) |
|
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Wed Mar 25, 2009 9:25 am |
|
|
hello again.
Let me explain,
I have a ps-2 keyboard connected to the pic. I convert scan code to ascii etc and I end up with the pressed key in int8 ascii number.
I want to add each character received to a string using the strcat command and finally have a string with the characters received.
That is why I want to convert number to string.
does "toascii" return char or int type? and please give me an example of how to use the macro.
Thanks. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Mar 25, 2009 9:42 am |
|
|
Basically you don't need a conversion cause int8 and char are alias types. They may be either signed or unsigned, but this doesn't matter in this regard.
The intended method of concatenating characters to a string is actually the more interesting thing. You may e.g. use an index:
Code: | char c[11]; // need room for terminating 0
int8 scancode;
for (i=0;i<10;i++)
{
scancode = get_it_somewhere();
c[i]=scancode;
}
c[10]=0;
printf("This is my collected string: %s\r\n",c); |
|
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Wed Mar 25, 2009 10:17 am |
|
|
Thank you FvM.
really simple indeed. It works!
Now I need to parse the string for expected names,
For example you write"print hello" to the keybord.
I want to search the string that we created, seperate and recognize the "print", the " " and the user input "hello".
As you can see I am trying to make a simple PC.
I already have a dspic running a VGA monitor at 640X480, creating fonts etc.
Now I have to analyze user input.
Regards. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Mar 25, 2009 2:19 pm |
|
|
Basically, CCS C is supporting standard C string library functions. They can achieve the intended action, e.g. strncmp() or strstr(). However, you have to pay attention to the fact, that constant strings (residing in flash memory) can't be used in all places where a string argument is required. Thus strstr(stri,"hello") doesn't work in default const mode. |
|
|
georpo
Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece.
|
|
Posted: Wed Mar 25, 2009 2:42 pm |
|
|
can you give me an example of the syntax for these commands?
thanks. |
|
|
|