This question already has answers here:
What's wrong with this expression? Cannot implicitly convert type 'int' to 'byte'
                                
                                    (5个答案)
                                
                        
                                3年前关闭。
            
                    
我正在尝试将char数组转换为字节。但是我收到以下错误:


  无法将int隐式转换为字节


public byte[] asciiToDecConversion(char[] asciiCharArray)
{
    byte[] decimalArray = new byte[10];
    const byte asciiFormat = 32;

    for (int j = 0; j < 10; j++)
    {
        decimalArray[j] = (Convert.ToByte(asciiCharArray[j]) - asciiFormat);
    }

    return decimalArray;
}

最佳答案

您需要强制转换为字节:

decimalArray[j] = (byte) (Convert.ToByte(asciiCharArray[j]) - asciiFormat);

关于c# - 将char转换为字节,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39923780/

10-11 12:27