Hex String Function

This is a discussion on Hex String Function within the C++ Programming forums, part of the General Programming Boards category; What this function (hex_str) does is, it receives a 1-byte char as argument, and returns its hex representation as string. ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Hex String Function

    What this function (hex_str) does is, it receives a 1-byte char as argument, and returns its hex representation as string.

    I'd like some tips on how to improve my code.

    Code:
    #include <iostream>
    #include <string>
    #include <stdio.h>
    
    using namespace std;
    
    char first_4_bits_hex(char value)
    {
        char hex_value[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    
        int count = 0;
        for(int x = 1; x <= 8; x = (x << 1))
        {
            if(value & x)
                count += x;
        }
    
        return hex_value[count];
    }
    
    string hex_str(char value)
    {
        string return_str="00";
        return_str[1] = first_4_bits_hex(value);
        value = value >> 4;
        return_str[0] = first_4_bits_hex(value);
        return return_str;
    }
    
    int main()
    {
        string str = hex_str(0x10);
        cout<<str;
        return 0;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    The effect of your for loop is the same as masking value with 0x0F.
    Also, you could shorten that char array initialisation to just "0123456789ABCDEF" 'i','n','s','t','e','a','d'.
    That function can be a one-liner when you know enough C++.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    The effect of your for loop is the same as masking value with 0x0F.
    Please, give me an example.

    Also, you could shorten that char array initialisation to just "0123456789ABCDEF" 'i','n','s','t','e','a','d'.
    How could I not have seen it before. Thanks. I'll apply it to my code.

    That function can be a one-liner when you know enough C++.
    Yeh, I'm sure there's many ways of doing this much better than what I did, that's why I posted here, the code's working, but maybe with the help of veterans I could improve it.
    Last edited by dhuan; 06-27-2011 at 01:38 AM.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Bump

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Whilst I wont write the exact answer for you, this should demonstrate how it can be done:
    Code:
    return "0IVXLCDM"[numeral & 0x07];
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2008, 11:42 PM
  2. what string function??
    By socket in forum C Programming
    Replies: 3
    Last Post: 03-04-2008, 07:35 PM
  3. Replies: 9
    Last Post: 04-16-2007, 04:02 PM
  4. Function of string ???
    By peter_hii in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2006, 10:04 AM
  5. Replies: 4
    Last Post: 01-22-2002, 11:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21