Thread: Printing 28 bits of a 32 bit unsigned char in C.

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    38

    Printing 28 bits of a 32 bit unsigned char in C.

    I am looking for some help with printing a only the first 28 significant bits in a 32 bit unsigned char.Here is some code that i have done so far.
    Code:
    void printHexOut(unsigned char *keyC, int Index[]) {
        int i;    for (i = 0; i < 4; i++) {
            if (i == 3) {
                fprintf(stderr, "%1x", (unsigned char)(keyC[i]));
            }
            else {
                fprintf(stderr, "%02x", (unsigned char)keyC[i]);
            }
                }
        fprintf(stderr,"\n");
         }
    When I print this, I get what I need for the 1st 6 hex characters, or 12 bits, but the last byte will always print out a 2 digit hex character (8 bits) instead of only 1 hex character (4 bits). Anyone know how I can accomplish this? Thank you in advance.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I'd probably use bitwise masks and print out each hex digit using my own code; not using printf() at all

    e.g. have something like

    const char *hdigits = "0123456789ABCDEF";

    and look up and print each digit (e.g. putchar(hdigits[idx]) in a loop where the masking (and shifting) is used to calculate idx)

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    Im not sure what you mean by that. I need to print to stderr anyway, so putchar() won't work for me. Is there a way to grab the 1st 4 bits of keyC[i] and determine the hex character? I would like to stay away from grabbing the hex printout and then treating each character separate, then only printing the 1st one. Seems un necessary.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I didn't see stderr (my mistake). fputc() is the function you need when you want to specify the stream (include <stdio.h>)

    Code:
    int fputc(int c, FILE *stream);
    As for "grabbing" the lower four bits (and for that matter, the upper 4) of an unsigned char (which is at least 8 bits; the actual number of bits is specified by the macro CHAR_BITS which is defined in limits.h... on all current x86 architecture that I'm aware of it's 8 bits) you would have to use the bitwise-and operator (&) and the bitwise-shift-right operator (>>)

    E.g.

    Code:
    unsigned char n, lower_bits, upper_bits;
    lower_bits = n & 0xf;
    upper_bits = (n >> 4) & 0xf;
    assert(lower_bits <= 0xf);
    assert(uppers_bits <= 0xf);
    You would use the value of lower_bits, or upper_bits (whichever is appropriate), as the index into the digit string/array I suggested earlier. Also, I wouldn't normally write the code as I have above but I think it's a better way for an example.

    Is this for an assignment or a personal project?


    Edit: There are many reasons for avoiding printf() when you don't need it (e.g. it's a large function and if space is a premium -- maybe on an embedded device -- then not having printf() included dramatically decreases the size of the object code). Others, and you, may disagree with me but I normally only use if it makes sense. For printing individual characters it doesn't make sense, to me. BUT I concede that outputting using the modifier %x is convenient... if you can get it to work how you want it to and in this case I'm not sure you can
    Last edited by Hodor; 03-22-2016 at 01:00 AM.

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    Not sure why I would do that when I have already iterated through each byte and am only having an issue printing the last byte. This is exactly what I was looking for. Its a personal project.

    Code:
    void printHexOut(unsigned char *keyC, int Index[]) {
        int i;    for (i = 0; i < 4; i++) {
            if (i == 3) {
                fprintf(stderr, "%1x", (unsigned char) (((keyC[i]) >> 4) & 0xf));
            }
            else {
                fprintf(stderr, "%02x", (unsigned char) keyC[i]);
            }
                }
        fprintf(stderr,"\n");
         }
    

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Yes, but I don't think the %x modifier can print a single hex-digit rather than 2. If I'm wrong someone will correct me, but code -- or a function called print_hexdigit() -- would be very easy to write and take about 30 seconds. I suspect that this is what you will have to do but I'm happy to be proved wrong.

    Edit:

    I can write the code for you, but it's not really the best way to learn. I'm sure I've given you enough information to proceed though.

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    With this modification, I was able to get it to print out
    Code:
     9802caf2e0eff9
    instead of
    Code:
     9802caf02e0eff90

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by TyrantUT View Post
    With this modification, I was able to get it to print out
    Oh ok. So the problem is solved using just the shift and mask? If so, cool (I guess you can tell that I don't use printf() for stuff like this very often hehe)

    You may want to do it the other way as well, just for fun and learning though

  9. #9
    Registered User
    Join Date
    Feb 2016
    Posts
    38
    I typically ONLY use fprintf. lol. Ill look into the way you described, may come in handy for other projects. Thank you for the help though.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I am looking for some help with printing a only the first 28 significant bits in a 32 bit unsigned char. ... Anyone know how I can accomplish this?
    Code:
    value = (unsigned long)(keyC[0] << 20) | (keyC[1] << 12) | (keyC[2] << 4) | (keyC[3]);
    printf("%x", value);
    You can also shift the high bits down on key[3] if for some reason that is where the important info is.
    Last edited by whiteflags; 03-22-2016 at 05:01 AM.

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I'm actually not even sure why key is an unsigned char *... it seems, to me, like really bad practice considering the problem description.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I agree it's inconvenient but I never bothered to ask what he's really doing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How covert unsigned char[4] to unsigned integer?
    By barracuda in forum C Programming
    Replies: 110
    Last Post: 02-23-2015, 04:00 AM
  2. Clearing all bits in an unsigned char
    By sigur47 in forum C++ Programming
    Replies: 4
    Last Post: 02-20-2012, 05:23 AM
  3. Replies: 5
    Last Post: 10-20-2010, 12:19 PM
  4. Char Help! "Packing " bits to a signle unsigned char
    By xxrexdartxx in forum C Programming
    Replies: 7
    Last Post: 10-11-2009, 04:45 AM
  5. Replies: 2
    Last Post: 10-06-2009, 09:37 AM