Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/03/07 02:15
Read: times


 
#134178 - Base conversions
Responding to: ???'s previous message
Is all about how numbers are represented as strings.

you need to convert .. say a decimal 5 to ASCII '5' etc.

Hint ASCII '0' = 0x30.... so '5' = 5 + '0' = 0x35


Eg 45 decimal....

break it down.....
0 * 100 + .... no hundreds
4 * 10 + ..... 4 tens
5 * 1 ...... 5 ones

so.... 45 becomes '0' '4' '5' ascii (0x30 0x34 0x35)


C code example

void main (){
int number = 45; // the number to be converted to ascii
int wk_number = number; // we will manipulate this variable
int hundreds,tens,ones;
unsigned char hun_char,tens_char,ones_char;

hundreds = (wk_number / 100); // computer how many hundreds

wk_number = wk_number - (hundreds* 100); // subtract hundreds result

tens = (wk_number / 10); // compute how many tens

wk_number = wk_number - (tens * 10); // subtract the tens result

ones = (wk_number / 1);

// convert them to ASCII characters

hun_char = (unsigned char) (hundreds) + '0'; // offset to ascii char

tens_char = (unsigned char) (tens) + '0'; // tens char

ones_char = (unsigned char) (ones) + '0'; // ones char
}


There are better ways to write this example.. but it gives you an idea.

Joe



List of 11 messages in thread
TopicAuthorDate
Help Data Type Conversion            01/01/70 00:00      
   Iterate            01/01/70 00:00      
      Don't understand            01/01/70 00:00      
         Base conversions            01/01/70 00:00      
            You gonna take his mid-terms too?            01/01/70 00:00      
               Sorry not homework            01/01/70 00:00      
                  Okay, her you go...            01/01/70 00:00      
      iterateration            01/01/70 00:00      
         Perhaps a bit more caution?            01/01/70 00:00      
   How would you do it on a PC?            01/01/70 00:00      
      Probably without using an 8052...            01/01/70 00:00      

Back to Subject List