将C#代码转换为C ++:十六进制到二进制

时间:2013-07-29 17:11:35

标签: c# c++ visual-studio-2010 visual-c++


大家好,

我有一些C#代码,并希望将其转换为C ++。我一直在尝试和谷歌搜索几种不同的方式,但是这个代码片段只是一些简单的修复,使其与C ++兼容?我是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;



    }

当我将sHexavalue更改为大写和ToCharArray时出现我的错误。我接收到这个错误:IntelliSense:初始化为'{...}'期望聚合对象

所以我知道从C#到C ++还有很多“转换”,但我只需要知道是否可以快速简单地转换代码片段。

编辑:这是原始来源:

//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;
    }

1 个答案:

答案 0 :(得分:2)

考虑一些建议:

  1. 由于sHexValue只读输入参数,因此传递const引用( const & ):

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

    (它比按值传递std::string更有效。)

  2. 无需在C ++中进行ToCharArray() C#转换 您可以使用 char 访问std::string中的单个operator[]

  3. 您可以使用多个A-F转换来管理case的大写和小写:

    case 'A': case 'a': .... break; // valid for both 'A' and 'a'
    
  4. 这是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;
    }