0

大家好,

我有一些 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;
    }
4

1 回答 1

2

考虑一些建议:

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

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

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

  2. 无需ToCharArray()在 C++ 中进行 C# 转换。您可以在using
    中访问单个chars 。std::stringoperator[]

  3. 您可以管理大小写以A-F使用多个case直通:

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

这是 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;
}
于 2013-07-29T17:36:11.757 回答