Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8619959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:27:57+00:00 2026-06-12T06:27:57+00:00

I am trying to print out an unsigned int as a hex, but not

  • 0

I am trying to print out an unsigned int as a hex, but not simply using %08X. I need to print the hex of the MSB and the LSB for each byte with the MSB first. I have gotten that:

unsigned int num = 1234567890;
char *p = (char*)#
for (int i = 0; i < 4; i++) {
    printf("%X%X", (*(p+i) & 0xF0) >> 4, *(p+i) & 0x0F);
}
// prints: D2029649

However I am having trouble going back to an unsigned int from a string containing D2029649:

short dehex(char hexLetter) {
  if(hexLetter > 47 && hexLetter < 58) hexLetter -= 48;
  if(hexLetter > 64) hexLetter -= 55;
  return hexLetter;  
}

unsigned int o = 0;
char *in = "D2029649";
int len = strlen(in);
for(int i = 0; i < len; i++) {
    if(i % 2 == 0) {
        o |= ((unsigned char)dehex(in[i]) << 4) + dehex(in[i+1]);
        o <<= 8;
    }
}

Needless to say, this doesn’t work at all. The two lines of code manipulating o are the result of my attempts to undo what the original encoding does and some research I did on how to turn bytes back into an int.

Perhaps my trouble comes with a complete misunderstanding of what I am actually doing when I am generating the hex.

Does anyone have any tips to get me going in the right direction to take the hex back to the original unsigned int?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:27:58+00:00Added an answer on June 12, 2026 at 6:27 am

    The purpose of your exercise appears to be converting the integer to a string, based on its memory layout, and then converting it back.

    Otherwise, you wouldn’t fiddle around with trying to figure out whether you’re on a byte boundary or not and then doing bit-shift “gymnastics”. Rather, you’d just just left shift the value by four and add the next digit in, something like:

    #include <stdio.h>
    #include <string.h>
    
    short dehex (char hexLetter) {
        if ((hexLetter >= '0') && (hexLetter <= '9'))
            return hexLetter - '0';
        return hexLetter - 'A' + 10;
    }
    
    int main (void) {
        unsigned int o = 0;
        char *in = "D2029649";
        int len = strlen (in);
        for (int i = 0; i < len; i++)
                o  = (o << 4) + dehex (in[i]);
        printf ("%X\n", o);
        return 0;
    }
    

    You’ll notice I’ve changed the dehex function slightly since I’m not a big fan of magic numbers where character constants are much more readable (it’s still not portable to all implementations, such as EBCDIC, but there’s precious few of those around nowadays).

    This program will output the unsigned integer value from that string in uppercase hex for checking: D2029649.

    However, the value 1234567890 decimal is, in hex, 499602D2, with the order of the bytes reversed from what you have in your string. That’s because you’re on what’s known as a little-endian architecture.

    So you have two problems.

    The first is that you’re processing the bytes in the wrong order for getting back your original number.

    You need to start at the end of the string and work backwards, something like:

    for (int i = len - 2; i >= 0; i -= 2) ...
    

    The second is that you’re doing the add/shift operations in the wrong order.

    Because your original code shifted after the add, it was getting to D2029649 and then shifting that left by eight bits resulting in 02964900 – the D2 shifts off the left into oblivion and a zero byte is shifted in at the right. You need to shift then add:

    o <<= 8;
    o |= ((unsigned char)dehex(in[i]) << 4) + dehex(in[i+1]);
    

    Finally, though not necessary, you can simplify your code quite a bit if you just use unsigned char for returning the converted nybble. The code I would write would be along the following lines:

    #include <stdio.h>
    #include <string.h>
    
    unsigned char dehex (char hexLetter) {
        if ((hexLetter >= '0') && (hexLetter <= '9'))
            return hexLetter - '0';
        return hexLetter - 'A' + 10;
    }
    
    int main (void) {
        unsigned int num = 1234567890;
        char *p = (char*) &num;
        for (int i = 0; i < 4; i++)
            printf("%X%X", (*(p+i) & 0xF0) >> 4, *(p+i) & 0x0F);
        putchar ('\n');
    
        unsigned int o = 0;
        char *in = "D2029649";
        int len = strlen (in);
        for (int i = len - 2; i >= 0; i -= 2) {
            o <<= 8;
            o |= (dehex (in[i]) << 4) + dehex (in[i+1]);
        }
        printf ("%X, or %u\n", o, o);
        return 0;
    }
    

    Note that that makes a lot more sense in terms of doing it a byte at a time rather than a nybble at a time. With nybble-based code, you would have to do array indexes in the order 6, 7, 4, 5, 2, 3, 0, 1 rather than a nice little 0..7 loop.

    The output of this program is:

    D2029649
    499602D2, or 1234567890
    

    showing that you’re getting back your original number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to print out the names of each person in the loop.
I'm using Django templating with Google App Engine. I'm trying unsuccessfully to print out
I'm trying to print out a list of products on a page. This is
I am trying to print out true if there is such letter/word and false
I am trying to print out documents in various different orders. Right now I
I am having trouble with my Objective-C code. I am trying to print out
I'm basically trying to iterate through a dict and print out the key /
I'm trying to come up with an algorithm that will print out all possible
I'm trying to write a very simple program, I want to print out the
I'm trying to iterate through all nodes, so I can print them out for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.