From "unsigned char" to Decimal using printf

0

I have the following code by which I connect to a sensor and I receive data from it that I keep in variables type "unsigned char"

    #include <18f2550.h>
    #fuses   HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
    #USE     delay(clock=48000000)
    #use i2c(Master,Fast,sda=PIN_B0,scl=PIN_B1)

    #include <usb_cdc.h>


 void main() {

 i2c_start();
 i2c_write(0x54);
 i2c_write(0x00);
 i2c_write(0x83);
 i2c_write(0x54);
 i2c_write(0x00);
 i2c_write(0x03);
 i2c_stop();

 usb_cdc_init();
 usb_init();

       while(TRUE) {

           delay_ms(720);
           i2c_write(0x54);
           i2c_write(0x03);
           i2c_write(0x55);

           unsigned char RM=i2c_read(1);
           unsigned char RL=i2c_read(1);
           unsigned char GM=i2c_read(1);
           unsigned char GL=i2c_read(1);
           unsigned char BM=i2c_read(1);
           unsigned char BL=i2c_read(1);
           unsigned char IM=i2c_read(1);
           unsigned char IL=i2c_read(0);
           i2c_stop();

           usb_task();


               if (usb_enumerated()) {


                     printf(usb_cdc_putc,"%d",RM);
                     printf(usb_cdc_putc,"%d",RL);                        
                     printf(usb_cdc_putc, "\f ");

               }

       }

   }

RM are the 8 most significant bits of the data and RL the least significant. I would like to know how to express this data as a total in Decimal using the command "printf".

For example if I get the data RM = 11111111 and RL = 11111111 that will appear on the screen as 65535.

Thank you very much.

    
asked by Alberto 17.12.2018 в 13:04
source

1 answer

1

The conversion of char to int is implied ; C internally always works with data of at least int .

Therefore, you only have to make a or binary between your data:

printf( "%d", (dato << 8) | dato2 );

Here ENDIAN comes into play, so if it does not give you the expected result, simply exchange your data:

printf( "%d", (dato2 << 8) | dato );

Notice that I use "%d" . This is how C works with the unsigned numbers; if you try to use "%u" , you will see a result ... curious ; -)

    
answered by 17.12.2018 в 13:23