I am having trouble understanding what happens in the 8051 Memory when numbers are stored as Char types. As I understand it when the letter A is store as a Char type the memory holds 0x41. When the number 191 is stored as a Char Type I would expect the memory to hold 3 Hex digits in a string starting with 0x31 for the number 1. This is not what happens.In fact 191 is stored as 0XBF which is the Hex value for 191. I don't understand why we bother using numbers as Char if they are stored the same way Int types are stored?? Can someone explain this??
I don't understand why we bother using numbers as Char if they are stored the same way Int types are stored??
go to the nearest hardware store and see that the price of a small bucket is less than that of a large bucket.
what may confoose you is that 'C' names the smallest storage unit 'char' although storing a character is just one of many uses of that.
Erik
This has nothing specifically to do with the 8051 or Keil.
Computers deal only with numbers. It's up to the programmer how those numbers are interpreted.
You can interpret the number as a code to represent a character, or you can interpret it just as a numeric value.
You could also interpret it as a code to represent many other things; eg, days of the week, months of the year, colours, etc, etc,...
Yes, the fact that 'C' calls its smallest data type "char" can be a little confusing when it is not being used to represent a character - but it doesn't actually limit its use.
What you need to take care of is to distinguish a numeric value, eg 1234, from a string of characters "1234": the former is a single numeric value; the latter is a character '1' followed by a character '2' followed by a character '3' followed by a character '4' (and, in C, followed by a NUL to terminate the string).
OK Please be specific. What must I do to make Char type digits numbers and what must I do to make Char type digits characters??
whether what is stored in a 'char' is to be seen as a number or a character code is up to you.
Your question needs to be a bit more specific:
Do you mean if you have, say, the character '3', how to get the corresponding numeric value of three?
Or vice-versa?
Or what?
That byte of memory don't care if you assign 0x40 or '@' or 64. The compiler will think it is the same thing.
If you want to convert a number into the characters that presents the number (so convert the number 64 into the character '6' (0x36) and '4' (0x34) then you can use [s[n]]printf(). Functions like atoi() or strtol() can be used to convert a sequence of character digits into the number the digits forms.
Let's say I wanted to store in Code Memory the Character String 1,2 as 0x31 and 0x32. How would you do this. If you store 12 in code memory as type CHAR you will save 0x0C.
[any segment or other attribute info] char my_str[] = "12",
Note that the above will store a third character too - a '\0' string termination.
[any segment or other attribute info] char my_array[2] = { '1','2' };
No termination character since no C string involved.
The only bit of that which is specific to C51 is the "CODE memory" part.
For C51-specific details, see the C51 manual:
http://www.keil.com/support/man/docs/c51/c51_le_memareas.htm
The rest is standard 'C'...
Note that the following are all equivalent:
char my_array[2] = { '1','2' };
char my_array[2] = { 0x31,0x32 };
char my_array[2] = { 49,50 };
char my_array[2] = { 061,062 };
char my_array[2] = { 0x31,'2' };
Great this makes sense now THANX