字典字符串值中的错误编译常量值

时间:2013-08-27 16:40:27

标签: c# winforms dictionary

我正在尝试制作一个字典,以便从Binary转换为Hex,这就是我所做的:

private Dictionary<string, char> binaryCharacterTohex = new Dictionary<string, char>
        {
            {'0000', "0"},
            {'0001', "1"},
            {'0010', "2"},
            {'0011', "3"},
            {'0100', "4"},
            {'0101', "5"},
            {'0110', "6"},
            {'0111', "7"},
            {'1000', "8"},
            {'1001', "9"},
            {'1010', "a"},
            {'1011', "b"},
            {'1100', "c"},
            {'1101', "d"},
            {'1110', "e"},
            {'1111', "f"}
        };

但它一直给我“错误的编译常量值”我放在那里的每个二进制字符串值('XXXX'),任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:2)

单引号在C#中用于表示字符,双引号表示字符串。你现在就把它们倒退了。将它们全部切换出来它应该可以正常工作,如下所示:

private Dictionary<string, char> binaryCharacterTohex = new Dictionary<string, char>
{
    {"0000", '0'},
    {"0001", '1'},
    {"0010", '2'},
    {"0011", '3'},
    {"0100", '4'},
    {"0101", '5'},
    {"0110", '6'},
    {"0111", '7'},
    {"1000", '8'},
    {"1001", '9'},
    {"1010", 'a'},
    {"1011", 'b'},
    {"1100", 'c'},
    {"1101", 'd'},
    {"1110", 'e'},
    {"1111", 'f'}
};

答案 1 :(得分:1)

'.'创建字符字面值 字符只能是一个字符。

你想要一个带有双引号的字符串文字。

答案 2 :(得分:1)

您倒置stringchar引号。

' '定义了char" "string

您需要将{'0000', "0"},替换为{"0000", '0'},

答案 3 :(得分:1)

双引号:""用于包装字符串。单引号:''用于字符。您的编译器错误意味着您已在代码中向后移动它们。您需要使代码看起来像这样:

private Dictionary<string, char> binaryCharacterTohex = new Dictionary<string, char>
        {
            {"0000", '0'},
            {"0001", '1'},
            //and so on
        };