Reverse a hex number in C

Table of Contents

  1. Code
  2. Header Inclusion
  3. Function Definitions
  4. Overall Functionality

1. Code:

Statement

Reverse a hexadecimal number like 0x12345678 into 0x78563412 by reversing its bytes (not just the bits or nibbles)

#include "stdint.h"
#include "stdio.h"

uint8_t Find_Number_Of_Bytes_Non_Zero(uint32_t hex)
{
    uint8_t countBytes = 0;

    while(hex != 0)
    {
        hex = hex >> 8;
        countBytes++;
    }

    return countBytes;
}

uint32_t Reverse_Hex_Bytes(uint32_t hex, size_t numBytes) 
{
    uint32_t reversed = 0;

    for (size_t i = 0; i < numBytes; i++) {
        unsigned int byte = (hex >> (i * 8)) & 0xFF;
        reversed |= byte << ((numBytes - 1 - i) * 8);
    }

    return reversed;
}

int main() {
    uint32_t hex1 = 0x123456;
    uint32_t hex2 = 0x12345678;

    size_t numBytes1 = Find_Number_Of_Bytes_Non_Zero(hex1);
    size_t numBytes2 = Find_Number_Of_Bytes_Non_Zero(hex2);

    uint32_t reverse1 = Reverse_Hex_Bytes(hex1, numBytes1);
    uint32_t reverse2 = Reverse_Hex_Bytes(hex2, numBytes2);

    printf("Original 16-bit: 0x%X, Reversed 16-bit: 0x%X\n", hex1, reverse1);
    printf("Original 32-bit: 0x%X, Reversed 32-bit: 0x%X\n", hex2, reverse2);

    return 0;
}

2. Header Inclusion:

#include : Includes the standard input/output library for functions like printf.
#include : Includes the standard integer types library for defining fixed-width integer types like uint8_t and uint32_t.

3. Function Definitions:

1. Find_Number_Of_Bytes_Non_Zero(uint32_t hex):

    • Purpose: Determines the number of non-zero bytes in a given 32-bit hexadecimal value.
    • Parameters:
      • hex: The 32-bit hexadecimal value to analyze.
    • Return Value:
      • The number of non-zero bytes in hex.
    • Logic:
      • Initializes countBytes to 0.
      • While hex is not zero:
        • Shifts hex right by 8 bits to examine the next byte.
        • Increments countBytes.
      • Returns countBytes.

2. Reverse_Hex_Bytes(uint32_t hex, size_t numBytes):

    • Purpose: Reverses the byte order of a given 32-bit hexadecimal value.
    • Parameters:
      • hex: The 32-bit hexadecimal value to reverse.
      • numBytes: The number of bytes to consider in the reversal.
    • Return Value:
      • The reversed hexadecimal value.
    • Logic:
      • Initializes reversed to 0.
      • Iterates numBytes times:
        • Extracts the current byte from hex using bitwise operations.
        • Shifts the extracted byte to its new position in reversed.
        • Or-assigns the shifted byte to reversed.
      • Returns reversed.


4. Main Function:

  • Purpose: Demonstrates the usage of the Find_Number_Of_Bytes_Non_Zero and Reverse_Hex_Bytes functions.
  • Logic:
    • Defines two 32-bit hexadecimal values, hex1 and hex2.
    • Calculates the number of non-zero bytes in each value using Find_Number_Of_Bytes_Non_Zero.
    • Reverses the byte order of each value using Reverse_Hex_Bytes with the appropriate number of bytes.
    • Prints the original and reversed values for both hex1 and hex2.

5. Overall Functionality:

The code effectively performs the following tasks:

  • Determines the number of non-zero bytes in a given hexadecimal value.
  • Reverses the byte order of a hexadecimal value based on the number of non-zero bytes.
  • Demonstrates these functions with example values.
  • The Find_Number_Of_Bytes_Non_Zero function is particularly useful when dealing with hexadecimal values that may have leading zero bytes, as it ensures that only the significant bytes are considered for reversal.