当PLC内寄存器的数值为一个负数时,比如-1,我们通过一些dll只能以uint16类型读取,则读取的数据为65535,这显然不符合我们的使用。
若使用Convert.ToInt16()进行转换,则VS会提示错误。
通过以下方式,即可实现转换。
static void Main(string[] args)
{
Int16 num=0;
while(true)
{
Console.WriteLine("请输入一个UInt16类型的数据:");
string str = Console.ReadLine();
UInt16 intputU16 = Convert.ToUInt16(str);
num = (Int16)(-1 & intputU16);
Console.WriteLine(num);
}
}