How to Increment Hexadecimal Characters in C

  • Thread starter sandy.bridge
  • Start date
In summary: Serial.begin(9600); //enable global interruptsSYSCTL_RCGC2 = 1;}void loop() {i = SYSCTL_RCGC2;}In summary, you can increment a hexadecimal character in C by using a variable with a numeric type to represent it, and then printing it out in hexadecimal.
  • #1
sandy.bridge
798
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.
 
Technology news on Phys.org
  • #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:
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.
 
  • #3
sandy.bridge said:
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.
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).
 
  • #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:
void adder (void) {
 unsigned int i = 0x10;
	GPIO_PORTA_DATA = i;
	
 while(i <= 0xF0)
 {
	 wait_for_key();
	 i += 0x10;
	 delay();
	
 }
}
 
Last edited:
  • #5
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.
 
  • #6
Okay, thanks.
 
  • #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.
 
  • #8
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:
  • #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:
#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:
  • #10
sandy.bridge said:
I wanted to increment 00000000 as such 00010000, then 00100000, and so forth, until 0xF0.
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:
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.
sandy.bridge said:
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.
 
  • #11
Thanks for the response. Perhaps something like this is what you are referring to?

Code:
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;}
 }
}
 
  • #12
sandy.bridge said:
Thanks for the response. Perhaps something like this is what you are referring to?

Code:
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;}
 }
}

More like this:
Code:
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.
 

Related to How to Increment Hexadecimal Characters in C

What is a hexadecimal character?

A hexadecimal character is a base-16 digit that can represent numbers from 0 to 15. In C, they are commonly used to represent binary numbers in a more compact form. For example, the hexadecimal character "A" represents the binary number 1010.

How do I increment a hexadecimal character in C?

To increment a hexadecimal character in C, you can use the increment operator "++". This will increase the value of the character by 1. For example, if you have the character "A", using "++" will result in "B".

Can I increment hexadecimal characters in a loop?

Yes, you can increment hexadecimal characters in a loop by using the "++" operator within the loop. This can be useful for tasks such as converting binary numbers to hexadecimal or vice versa.

What happens when I increment the hexadecimal character "F"?

When you increment "F", it will become "10". This is because in hexadecimal, after "9" comes "A" and "F" is the last character before "10". This behavior is consistent with the base-10 system, where after "9" comes "10".

How do I convert a hexadecimal character to a decimal number in C?

In C, you can use the strtol() function to convert a hexadecimal character to a decimal number. This function takes in a string containing the hexadecimal character and returns its decimal equivalent.

Similar threads

  • Programming and Computer Science
Replies
18
Views
3K
  • Programming and Computer Science
Replies
32
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
30
Views
4K
  • Precalculus Mathematics Homework Help
Replies
8
Views
854
  • Programming and Computer Science
Replies
7
Views
2K
  • Computing and Technology
Replies
4
Views
1K
  • Biology and Chemistry Homework Help
Replies
1
Views
971
Back
Top