大家好,
我有一些 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;
}