简体   繁体   English

在C#中以相同的结果将Binary(16)转换为int

[英]Converting Binary(16) to int with same result in C#

I have Binary(16) column in table 'Chip' with value 0xE1FC2E6F8674B7B9045C1104F9124C48 and in another table i have column chip_i which is type of integer that has the same value (but in int) = -116241336. 我在表'Chip'中的Binary(16)列的值为0xE1FC2E6F8674B7B9045C1104F9124C48,在另一个表中,我的列chip_i是具有相同值的整数类型(但int)= -116241336。 Im using SQL Server 2012. 我正在使用SQL Server 2012。

How can i convert 0xE1FC2E6F8674B7B9045C1104F9124C48 to -116241336 in C#? 如何在C#中将0xE1FC2E6F8674B7B9045C1104F9124C48转换为-116241336?

I tried to convert it like this: 我试图这样转换它:

  string hexString = "0xE1FC2E6F8674B7B9045C1104F9124C48";
  byte[] hexByte = Encoding.ASCII.GetBytes(hexString);
  var chip_i = BitConverter.ToInt32(hexByte, 0);

but the result is 826636336 但结果是826636336

-116241336 is simply the last 4 bytes treated as a raw little-endian integer; -116241336只是将后4个字节视为原始的Little-Endian整数; 0xF9124C48 . 0xF9124C48 So: just use the last 4 bytes as is . 因此:只需按原样使用最后4个字节。 No need for ASCII: 无需ASCII:

int chip_i = Convert.ToInt32(hexString.Substring(hexString.Length - 8, 8), 16);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM