Find the answer to your Linux question:
Results 1 to 8 of 8
Assume i have a char which is 8 bits if i am not mistaken. I want to convert it to hexadecimal. I can do this easily by reading 4 bits ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2004
    Location
    Stalk much?
    Posts
    19

    Reading 4 bits


    Assume i have a char which is 8 bits if i am not mistaken.
    I want to convert it to hexadecimal. I can do this easily by reading 4 bits at a time. Can i like use *ptrtoint + .5? or something? if you have another way to convert it to hex, please let me know

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    No, there is no way to use "half pointers" or anything like you're suggesting. Pointers are integers.

    Instead, you'll have to use bit-wise boolean operations. For example, like this:
    Code:
    char nibble2hex(int nibble) {
        if(nibble < 10)
            return(nibble + '0');
        else
            return(nibble + 'A');
    }
    
    void char2hex(int c, char *resbuf) {
        resbuf[0] = nibble2hex((c & 0xf0) >> 4);
        resbuf[1] = nibble2hex(c & 0x0f);
        resbuf[2] = 0;
    }
    Alternatively, you could just use printf:
    Code:
    void char2hex(int c, char *resbuf) {
        sprintf(resbuf, "%02x", c);
    }

  3. #3
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,845
    Quote Originally Posted by Dolda2000
    ...
    Alternatively, you could just use printf:
    Code:
    void char2hex(int c, char *resbuf) {
        sprintf(resbuf, "%02x", c);
    }
    If you're dealing with char sized elements here, make sure you call this using:

    Code:
    char ch = 0xF5;
    char buffer[128]="";
    char2hex((int)ch,buffer);
    'cos some compilers are a bit iffy about automatic conversions.
    Linux user #126863 - see http://linuxcounter.net/

  4. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    That will also have some side effects, though, due to sign extension (when the character is 0x80 or above). You'll need to specify that it is an unsigned char, either by using that type specification all along when declaring your char variables, or by a double cast, like "(int)(unsigned char)c".

  5. #5
    Just Joined!
    Join Date
    Jan 2006
    Location
    India
    Posts
    52
    Hi Dolda,

    char nibble2hex(int nibble) {
    if(nibble < 10)
    return(nibble + '0');
    else
    return(nibble + 'A');
    }
    In above programm you need to replace the else part with

    return ( nibble - 10 + 'A' );

    which will output the correct ascii value of the hex nubers ( A,B....F)

    rajesh

  6. #6
    Just Joined!
    Join Date
    Apr 2004
    Location
    Stalk much?
    Posts
    19
    sweet. That works perfectly!
    But could you explain how these lines work so i can do it in the future? :
    Code:
     resbuf[0] = nibble2hex((c & 0xf0) >> 4);
        resbuf[1] = nibble2hex(c & 0x0f);

  7. #7
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,845
    Quote Originally Posted by zappacky
    sweet. That works perfectly!
    But could you explain how these lines work so i can do it in the future? :
    Code:
     resbuf[0] = nibble2hex((c & 0xf0) >> 4);
        resbuf[1] = nibble2hex(c & 0x0f);
    Code:
    (c & 0xf0)
    uses a bitwise AND to take the top 4 bits from the byte, 0xF0 is 11110000 in binary.

    Code:
    >> 4
    mean bit-shift the result to the right by four bits (i.e. quickly divides by 16).

    Code:
    (c & 0x0f)
    means use a bitwise AND to take the bottom four bits from the byte, 0x0F is 00001111 in binary.
    Linux user #126863 - see http://linuxcounter.net/

  8. #8
    Just Joined!
    Join Date
    Jan 2006
    Location
    India
    Posts
    52
    That's right....

    Bit wise shift operators like '<<' , '>>'
    wiill always shifts the operand by specified numbers or multiply it by 2^n.

    rajesh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •