Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

sprintf hex in XC8

Status
Not open for further replies.

Nigel Goodwin

Super Moderator
Most Helpful Member
I'm having a bit of a 'mad' moment - I can't print an unsigned long (32 bit) as hex in XC8.

So I've cut and pasted the relevant section of code to an on-line C compiler, where it works fine:

C:
#include <stdio.h>

char text[9];

// Function to reverse bits of num
unsigned char reverseBits(unsigned char num)
{
    //unsigned char  NO_OF_BITS = sizeof(num) * 8;
    unsigned char reverse_num = 0, i, temp;
 
    for (i = 0; i < 8; i++)
    {
        temp = (num & (1 << i));
        if(temp)
            reverse_num |= (1 << ((7) - i));
    }
  
    return reverse_num;
}

int main() {
              unsigned long nec_code         = 0x18E708F7;
            unsigned long fullcode       = nec_code;
            unsigned int address16       = nec_code >> 16;
            unsigned int command16       = nec_code & 0xFFFF;
            unsigned char address        = nec_code >> 24;
            unsigned char inv_address    = nec_code >> 16;
            unsigned char command        = nec_code >> 8;
            unsigned char inv_command    = nec_code;
    
            // reverse codes to correct values
            command=reverseBits(command);
            inv_command=reverseBits(inv_command);
            address=reverseBits(address);
            inv_address=reverseBits(inv_address);
            //address16=reverseBits16(address16);

            sprintf(text,"%08X",fullcode);
            printf("Full code: 0x%s\n", text);   // print nec_code
    
            sprintf(text,"%04X",nec_code>> 16);
            printf("NEC code: 0x%s", text);   // print nec_code
            sprintf(text,"%04X",nec_code & 0xFFFF);
            printf("%s\n", text);   // print nec_code
            
            sprintf(text,"%04X",address16);
            printf("Address16: 0x%s\n", text);   // print address16
            
            sprintf(text,"%04X",command16);
            printf("Command16: 0x%s\n", text);   // print command16
    
            sprintf(text,"%02X",address);
            printf("Address: 0x%s\n", text);   // print address
    
            sprintf(text,"%02X",inv_address);
            printf("Inverted: 0x%s\n", text);   // print inv_address

            sprintf(text,"%02X",command);
            printf("Command: 0x%s\n", text);   // print command

            sprintf(text,"%02X",inv_command);
            printf("Inverted: 0x%s\n\r", text);   // print inv_command
}

Result:

Full code: 0x18E708F7
NEC code: 0x18E708F7
Address16: 0x18E7
Command16: 0x08F7
Address: 0x18
Inverted: 0xE7
Command: 0x10
Inverted: 0xEF
    
Same result in XC8:

Full code: 0x000008F7
NEC code: 0x18E708F7
Address16: 0x18E7
Command16: 0x08F7
Address: 0x18
Inverted: 0xE7
Command: 0x10
Inverted: 0xEF

The code is part of my current experimentation with infrared remotes, this time for an NEC one. The output of an NEC remote is 32 bits, the first 8 are the address, the second 8 the same but inverted, then the 8 bit command, followed by the command inverted. All least significant bit first, hence the routine for flipping their order, to give the 'proper' values.

I can strip the two 16 bit values out, and combine them to display the 32 bit value (second line), but why doesn't the first line work with XC8 (v2.36 in C90 mode)?.
 
I think the compiler has different defaults... The online will default to 32bit XC8 defaults to 16bit.

I know if I use the word int on a pic18 it defaults to unsigned, the same word defaults to signed int on a pic32

Portable eh!!! My arse!
 
I think the compiler has different defaults... The online will default to 32bit XC8 defaults to 16bit.

I know if I use the word int on a pic18 it defaults to unsigned, the same word defaults to signed int on a pic32

Portable eh!!! My arse!

I wasn't aware of that - and have always assumed it was signed? - I'll have to watch out for it.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top