0

Hello All,

I have some C# code and would like to convert it to C++. I have been trying and googling for several different ways, however is there just some easy fixes to this code snippet to make it C++ compatible? I am a novice is C++:

static std::string Hex2Bin(std::string sHexavalue)
    {

        std::string binaryValue = "";
        char charArray[] = sHexavalue::toupper.ToCharArray();

        for( int n=0; n < sHexavalue.length(); n++)
        {
            switch (charArray[n])
            {

                        case '0':
                            binaryValue += "0000";break;
                        case '1':
                            binaryValue += "0001";break;
                        case '2':
                            binaryValue += "0010";break;
                        case '3':
                            binaryValue += "0011";break;
                        case '4':
                            binaryValue += "0100";break;
                        case '5':
                            binaryValue += "0101";break;
                        case '6':
                            binaryValue += "0110";break;
                        case '7':
                            binaryValue += "0111";break;
                        case '8':
                            binaryValue += "1000";break;
                        case '9':
                            binaryValue += "1001";break;
                        case 'A':
                            binaryValue += "1010";break;
                        case 'B':
                            binaryValue += "1011";break;
                        case 'C':
                            binaryValue += "1100";break;
                        case 'D':
                            binaryValue += "1101";break;
                        case 'E':
                            binaryValue += "1110";break;
                        case 'F':
                            binaryValue += "1111";break;

            }

        }


        return binaryValue;



    }

My Error occurs when I have sHexavalue to be changed to uppercase and ToCharArray. I receieve this error: IntelliSense: initialization with '{...}' expected for aggregate object

So I know there is still lots of "conversion" from C# to C++ but I just need to know if it is possible to have the snippet converted in a quick simple way.

EDIT: Here is the original source:

//convert a Hexa value string to a binary value string
public static string Hex2Bin(string sHexavalue)
    {
        string binaryValue = "";
        char[] charArray = sHexavalue.ToUpper().ToCharArray();  

            for( int n=0; n < sHexavalue.Length; n++)
            {
                switch (charArray[n])
                {
                    case '0':
                        binaryValue += "0000";break;
                    case '1':
                        binaryValue += "0001";break;
                    case '2':
                        binaryValue += "0010";break;
                    case '3':
                        binaryValue += "0011";break;
                    case '4':
                        binaryValue += "0100";break;
                    case '5':
                        binaryValue += "0101";break;
                    case '6':
                        binaryValue += "0110";break;
                    case '7':
                        binaryValue += "0111";break;
                    case '8':
                        binaryValue += "1000";break;
                    case '9':
                        binaryValue += "1001";break;
                    case 'A':
                        binaryValue += "1010";break;
                    case 'B':
                        binaryValue += "1011";break;
                    case 'C':
                        binaryValue += "1100";break;
                    case 'D':
                        binaryValue += "1101";break;
                    case 'E':
                        binaryValue += "1110";break;
                    case 'F':
                        binaryValue += "1111";break;

                }

            }
        return binaryValue;
    }
2
  • Is this the original C# code? Commented Jul 29, 2013 at 17:14
  • @Borgleader, No it is not. I am trying to convert now and can provide the actual in an edit now. Commented Jul 29, 2013 at 17:15

1 Answer 1

2

Consider some suggestions:

  1. Since the sHexValue is a read-only input parameter, pass by const reference (const &):

    std::string Hex2Bin(const std::string & sHexValue)
    

    (It's more efficient than passing the std::string by value.)

  2. No need to do the ToCharArray() C# conversion in C++.
    You can access single chars in a std::string using operator[].

  3. You can manage the upper and lower case for A-F using multiple case fall-through:

    case 'A': case 'a': .... break; // valid for both 'A' and 'a'
    

Here's a possible rewrite in C++:

// Convert a hex value string to a binary value string.
std::string Hex2Bin(const std::string & sHexValue)
{
    std::string binaryValue;   // automatically initialized to empty string

    for (int n = 0; n < sHexValue.size(); ++n)
    {
        switch (sHexValue[n])
        {
            case '0':
                binaryValue += "0000"; break;
            case '1':
                binaryValue += "0001"; break;
            case '2':
                binaryValue += "0010"; break;
            case '3':
                binaryValue += "0011"; break;
            case '4':
                binaryValue += "0100"; break;
            case '5':
                binaryValue += "0101"; break;
            case '6':
                binaryValue += "0110"; break;
            case '7':
                binaryValue += "0111"; break;
            case '8':
                binaryValue += "1000"; break;
            case '9':
                binaryValue += "1001"; break;
            case 'A': case 'a':
                binaryValue += "1010"; break;
            case 'B': case 'b':
                binaryValue += "1011"; break;
            case 'C': case 'c':
                binaryValue += "1100"; break;
            case 'D': case 'd':
                binaryValue += "1101"; break;
            case 'E': case 'e':
                binaryValue += "1110"; break;
            case 'F': case 'f':
                binaryValue += "1111"; break;
        }
    }

    return binaryValue;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Wow that was an excellent solution! Thank you tremendously. I was going out of my mind just now..You are amazing. I was able to compile easily. I appreciate all of your suggestions :)
@Mr.C64: why did you leave the switch statement and not use a lookup table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.