Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C

Options
  • 25-08-2004 1:28pm
    #1
    Registered Users Posts: 2,191 ✭✭✭


    Im sorry this is a really stupid problem, but the comp I work on doesn't have help.

    In C when you use printf %d means int, %f means float but what does %x mean. It kinda looks like hexadecimal


Comments

  • Closed Accounts Posts: 370 ✭✭Darren


    Unpossible wrote:
    Im sorry this is a really stupid problem, but the comp I work on doesn't have help.

    In C when you use printf %d means int, %f means float but what does %x mean. It kinda looks like hexadecimal


    Correct, print in hex format.


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    how on earth do I declare a variable in hex? do I use unsigned char?


  • Closed Accounts Posts: 370 ✭✭Darren


    I think you declare it with a 0x at the beginning but I'll have to check one of my programmes on the mainframe. 2 mins.


  • Registered Users Posts: 205 ✭✭Stugots


    Your guess is correct - %x means hexadecimal (lower case). %X means hexadecimal upper case.

    A useful formatting addition is to use %02X (for an 8-bit hexadecimal) which formats the output as two characters wide and precedes it with a 0 to pad to two characters if necessary.


  • Registered Users Posts: 205 ✭✭Stugots


    There is no hex type as such in C, but you can initialise a variable to a hex value as follows:

    char myVariable1 = 0x01;

    int myVariable2 = 0xDEAD;


  • Advertisement
  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    so would
    unsigned char myBlue = camera_get_blue;
    work?


  • Closed Accounts Posts: 370 ✭✭Darren


    This is one of my old programmes

    #define EOF -1
    #include <stdio.h>
    main()
    {
    int a, x;

    a = 0x10;

    for (x = 0; x < 500; x++)
    { a = a + 1;
    printf( "%x %d\n", a, a); }
    return 0;


    }

    It declares a as being hex10 and the increments it and prits the hex value and corresponding dec values each time. hope it helps.


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    ahh so the piece above should be;

    char myBlue = 0x10;
    myBlue = camera_get_blue;

    wait how come your hex is an int but Stugots was char? Suppose it doesn't matter as long as I have the 0x10, does it?


  • Closed Accounts Posts: 44 Err..


    Hex is merely a representation of an integer. Therefore you can use any integer type (int, byte, char, etc.) as long as it is big enough to hold the value you want to store in it.

    The following two lines are equivalent:

    int i = 255;
    int i = 0xFF;


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    ok, thanks everyone for your help, sorry for asking something which I should have probably known (thank god Im getting out of software development, id starve in the real world)


  • Advertisement
  • Registered Users Posts: 205 ✭✭Stugots


    Unpossible wrote:
    so would
    unsigned char myBlue = camera_get_blue;
    work?

    On its own, this sample is not particularly useful. Is camera_get_blue a char value initialised elsewhere or perhaps a function that reads a value from a camera?
    It would clarify things a little to give a more complete example, e.g.

    void main()
    {
    unsigned char camera_get_blue = 5;
    unsigned char myBlue = camera|_get_blue;

    }

    or

    extern unsigned char camera_get_blue();

    void main()
    {
    unsigned char myBlue = camera|_get_blue();

    }

    Given the questions you're asking at this point, I think you could badly do with a reference manual of some type. Its going to be a very slow process to post every syntactic query on this board. "The C programming Language" by Kernighan and Richie is pretty much the bible. I also recommend "C The Complete Reference" by Herbert Schildt.


  • Closed Accounts Posts: 44 Err..


    If you are only dealing with a bunch of positive integers and know the largest you will have to deal with then, as good programming practice, you should pick the smallest unsigned integer type that will suffice. Note that each 2 characters in a hex vale = 1 byte.

    So,

    Range 0x00 - 0xFF - unsigned char
    Range 0x0000 - 0xFFFF - unsigned short

    etc.


  • Registered Users Posts: 2,191 ✭✭✭Unpossible


    camera_get_blue comes from a .h file


Advertisement