Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C and hexadecimals

  1. Oct 3, 2013 #1
    Hey guys,

    I was wondering if it were possible to increment a hexadecimal character in C? For example, it's rather easy to increment an integer value, however, every time I have seen a hexadecimal number it was data type 'character', and therefore I am not even sure if it is possible.

    Thanks for any clarifications.
     
  2. jcsd
  3. Oct 3, 2013 #2
    All numbers (generally speaking) are represented as binary inside the computer. What base you decide to use when displaying them is a separate thing. So really, if you want to increment a hex number in C, just use a variable with a numeric type to represent it, then print it out in hexadecimal (or octal, or whatever).

    For example, a code fragment which will initialize a variable of type 'unsigned int' with some hex number, increment, and display the number with printf:

    Code (Text):

    unsigned int some_number = 0xAF;

    some_number++;
    printf("0x%x\n", some_number);
     
    That's off the top of my head, so I could have made an error. Plus, the code around it to make it work is missing, but that's the gist of it.
     
  4. Oct 3, 2013 #3

    Mark44

    Staff: Mentor

    Sure, you can increment characters. The char type is one of the integral types, along with int, long, short, and the signed and unsigned variants.
    There's not really such a thing as a hexadecimal character. The underlying bit pattern remains the same, but how that pattern is displayed by the use of the format specifier in, say, printf.

    Using %c, the char displays as a normal character.
    Using %x, the char displays in hex.
    Using %o, the char displays in octal.
    Using %i, the char displays in decimal (its ASCII code).
     
  5. Oct 3, 2013 #4
    What would be the benefit in using the data type char over int? In my microprocessor class, it seems like all the examples use 'char' in storing the hex value.

    EDIT:

    I decided to just show the code that I cannot get to work. I do have it working with an array, but I cannot seem to get the increment to work with this function. Here:

    Code (Text):
    void adder (void) {
     unsigned int i = 0x10;
        GPIO_PORTA_DATA = i;
       
     while(i <= 0xF0)
     {
         wait_for_key();
         i += 0x10;
         delay();
       
     }
    }
     
    Last edited: Oct 3, 2013
  6. Oct 3, 2013 #5

    TumblingDice

    User Avatar
    Gold Member

    As Grep says, all numbers are binary when stored to caompute/calculate as numbers. I think the key to your question is the "hexadecimal character". You would see this only when the value is manipulated as text. Often that's for printing or display, when it's a human requirement to see the value in hex representation. So for example, a type char[8] could hold 8 hex characters. You can move binary values (i.e., int) into the text char using printf and format types as Mark44 listed.
     
  7. Oct 3, 2013 #6
    Okay, thanks.
     
  8. Oct 3, 2013 #7
    I wanted to increment 00000000 as such 00010000, then 00100000, and so forth, until 0xF0. I updated the code above, which I did not properly write down before (and it still is wrong). On the microcontroller, it is set up such that A3A2A1A0B3B2B1B0, and I wanted to count up in sequence on the A port only. I was able to accomplish the task at hand, but I did not do so via a counter. I am almost certain that my issue is coming from the first hexadecimal, but I am not sure if there is a way around that.
     
  9. Oct 4, 2013 #8

    TumblingDice

    User Avatar
    Gold Member

    A little Googling and I think maybe you're coding for micro ARM. I do desktop Windows - well, I could do more harm than good. Be warned. ;)

    GPIO_PORTA_DATA looks to be a #define for a memory/register location:
    #define * GPIO_BASE * * 0x1000F000
    #define GPIO_PORTA_DATA * * * * * (GPIO_BASE+0X018)

    Looking at where you set "GPIO_PORTA_DATA = i". That looks unusual to me. is that directly setting the memory location specified by GPIO_PORTA_DATA to 0x10?

    I hope someone else gives you a proper answer quickly. If you have time, I would enjoy seeing the code you say works w/o using the counter.
     
    Last edited: Oct 4, 2013
  10. Oct 4, 2013 #9
    Here is my code that worked. It's an up-counter. Port A has four pins that output a high value. I used a digilent analyzer to display the values, and it ends up counting in binary from 0 to 15, in sequence, every time a switch on the ARM controller is pressed. I'm just learning how to use this, so there is likely flaws in my coding.

    Code (Text):
    #define SYSCTL_RCGC2          (*((volatile unsigned long *)0x400FE108))
    //GPIO initialization
    #define GPIO_PORTF_DATA       (*((volatile unsigned long *)0x400253FC))
    #define GPIO_PORTF_DIR        (*((volatile unsigned long *)0x40025400))
    #define GPIO_PORTF_AFSEL      (*((volatile unsigned long *)0x40025420))
    #define GPIO_PORTF_DEN        (*((volatile unsigned long *)0x4002551C))
    #define GPIO_PORTF_PCTL       (*((volatile unsigned long *)0x4002552C))
    #define GPIO_PORTF_PUR        (*((volatile unsigned long *)0x40025510))
    #define GPIO_PORTA_DATA       (*((volatile unsigned long *)0x400043FC))
    #define GPIO_PORTA_DIR        (*((volatile unsigned long *)0x40004400))
    #define GPIO_PORTA_AFSEL      (*((volatile unsigned long *)0x40004420))
    #define GPIO_PORTA_DEN        (*((volatile unsigned long *)0x4000451C))
    #define GPIO_PORTA_PCTL       (*((volatile unsigned long *)0x4000452C))
     

    //declare functions
    void init_gpio (void);
    void wait_for_key (void);
    void delay (void);
    void initialConditions (void);
    void adder (void);


    //define functions
    void init_gpio (void) {
        volatile unsigned long delay_clk; // delay for clock, must have 3 sys clock delay
        SYSCTL_RCGC2 |= 0x00000023; // clock enable for port A,B,F,
        delay_clk = SYSCTL_RCGC2; // must delay for the clock to settle, no-operation
         
        GPIO_PORTF_DIR |= 0x00; // PF4 input, PF0 input, PF1-PF3 output
        GPIO_PORTF_AFSEL &= ~0xFF;// disable alt funct on PF0-PF4
        GPIO_PORTF_DEN |= 0x1F; // enable digital I/O on PF0-PF4
        //GPIO_PORTF_PCTL = (GPIO_PORTF_PCTL & 0xFFF00000) + 0x00000000;
        GPIO_PORTF_PCTL &= 0xFFF00000;
        GPIO_PORTF_PUR |= 0x11; // enable pull-up on PF4, PF0
     
     
    //Set port A
        GPIO_PORTA_DIR |= 0xFF; // PF set entirely to output
        GPIO_PORTA_AFSEL &= ~0x00;// disable alt funct on PA0-PA7
        GPIO_PORTA_DEN |= 0xFF; // enable digital I/O on PA0-PA7
    //GPIO_PORTA_PCTL = (GPIO_PORTF_PCTL & 0xFFF00000) + 0x00000000;
        GPIO_PORTA_PCTL &= 0xFFF00000;
    //GPIO_PORTA_PUR |= 0xFF; // enable pull-up on All
      }
     
    const char aRRay [15] =   {0x10,  0x20, 0x30, 0x40, 0X50,  0X60, 0x70, 0x80, 0X90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0 };

    void wait_for_key (void) {
      while(GPIO_PORTF_DATA & 0x10);
    }

    void adder (void) {
      int i = 0;
      GPIO_PORTA_DATA = 0x00;
       
      while(i <= 15)
      {
        wait_for_key();
        GPIO_PORTA_DATA = aRRay[i];
         i++;
        delay();
      }
    }

    void delay (void) {
     long unsigned i;
     for (i = 0; i < 1000000; i++);
     
    }
     
    //main functionc
    int main (void) {
          init_gpio ();
       
          while (1) {
            adder();
          }
    }
    I am not really in a rush. I was merely just looking for alternate ways that I could do this for in the future.
     
    Last edited by a moderator: Oct 4, 2013
  11. Oct 4, 2013 #10

    Mark44

    Staff: Mentor

    Switching back and forth between binary and hex is a little confusing, but I get what you're trying to do. If we stay in hex notation, you want to increment 0x0 to 0x10, then to 0x20, and so on, up to 0xF0. Each time you are incrementing by 0x10 (16 in decimal or 10000 in binary).

    That's easy enough to do.
    Code (Text):
    var += 0x10;
    You might want to add logic for the case when var == 0xF0 so that it goes around to 0x00, if that's what you need to have happen.
     
  12. Oct 4, 2013 #11
    Thanks for the response. Perhaps something like this is what you are referring to?

    Code (Text):
    void adder (void) {
     unsigned int i = 0x0;
        GPIO_PORTA_DATA = i;
       
     while(i <= 0xF0)
     {
         wait_for_key();
         i += 0x10;
         delay();
        if(i == 0xF0){i=0x00;}
     }
    }
     
  13. Oct 4, 2013 #12

    Mark44

    Staff: Mentor

    More like this:
    Code (Text):
    void adder (void)
    {
       unsigned int i = 0x0;
       GPIO_PORTA_DATA = i;
       
       while(i <= 0xF0)  
       {
          wait_for_key();
          if (i == 0xF0)
          {
             i = 0x0;
          }
          else
          {
             i += 0x10;
          }
          delay();
       }
    }
    Don't you need i to be of type unsigned char? I'm assuming you're working with bytes that are 8 bits. The int type on your microcontroller is probably 16 bits. If so, it's more than you need. Also, if you use unsigned char, I don't think you need the extra logic that I showed for when i == 0xF0. Incrementing i by 0x10 will overflow the byte and you'll be left with 0x0, which is what you want.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C and hexadecimals
  1. To C or not to C (Replies: 31)

  2. C or C++? (Replies: 8)

  3. Java or C/C ++ (Replies: 5)

Loading...