簡體   English   中英

如何將十進制轉換為十六進制字符串的十六進制和XOR

[英]How to convert decimal to hexadecimal and XOR of the hex string

我有一個字符串數組( string[] )作為十進制數字,如0, 0, 4, 142 ,我想要做的就是將數組轉換為十六進制數字,如0, 0, 4, 8e並在其中執行XOR C#,但我並沒有期望XOR

碼:

public CheckSumHelper(string[] array)
{
  this._array = array.Select(x => Convert.ToInt64(x, 16)).ToArray();
}

public string GetCheckSum()
{
   long xor = this._array.Aggregate((x, y) => x ^ y);
   return xor.ToString("X");
}

Convert.ToInt64(string,int)方法指定字符串輸入的基數。 您正在將“ 142”轉換為0x142,而不是0x8e。

只需使用Convert.ToInt64(string)

至於您可能遇到的XOR問題,請參閱這篇文章: 具有3個值的xor

基於您提供的數據和您要計算校驗和的想法,我懷疑您實際需要做的是提供縱向冗余檢查。 最簡單的方法是:

(SumOfAllData & 0xFF) ^ 0xFF + 1

翻轉數字中的所有位並加1的動作也被稱為二的補語。

示例代碼:

private int CalcChecksum(int[] array)
{
    int Sum = array.Aggregate(0, (total, value) => total += value);
    return ((Sum & 0xFF) ^ 0xFF) + 1;
}

由於142十進制數 (而不是十六進制數)(您不對待0x142 == 332 ),因此在Convert.ToInt64(...)刪除16

 public static string GetCheckSum(string[] array) {
   // TODO: validate array here (it must be not null, not empty etc.)

   return array
     .Select(item => Convert.ToInt64(item)) // initial number is decimal           
     .Aggregate((s, item) => s ^ item) 
     .ToString("X");                        // we want final result being hexadecimal
 }

因此,您將擁有0 ^ 0 ^ 4 ^ 142 == 138 == 0x8A

編輯:使用格式時,讓計算機解釋發生了什么:

private static string GetCheckSumExplained(string test) {
  string[] array = test.Split(',');

  // Routine under test - GetCheckSum 
  string result = GetCheckSum(array);
  // Convert string back to long in order to represent it as binary and decimal
  long resultAsInt = Convert.ToInt64(result, 16);

  string args = string.Join(Environment.NewLine, array
    .Select(item => Convert.ToInt64(item))
    .Select(item => $"{Convert.ToString(item, 2).PadLeft(8, '0')} : {item,3} : 0x{item.ToString("X2")}"));

  return string.Join(Environment.NewLine, 
     args,
    "---------------------",
   $"{Convert.ToString(resultAsInt, 2).PadLeft(8, '0')} : {resultAsInt,3} : 0x{result.PadLeft(2, '0')}");
}

...

string test = "0,0,4,20,15,142,0,8,179,141,0, 8, 181, 141, 0,8"; 

Console.Write(GetCheckSumExplained(test));

結果:

00000000 :   0 : 0x00
00000000 :   0 : 0x00
00000100 :   4 : 0x04
00010100 :  20 : 0x14
00001111 :  15 : 0x0F
10001110 : 142 : 0x8E
00000000 :   0 : 0x00
00001000 :   8 : 0x08
10110011 : 179 : 0xB3
10001101 : 141 : 0x8D
00000000 :   0 : 0x00
00001000 :   8 : 0x08
10110101 : 181 : 0xB5
10001101 : 141 : 0x8D
00000000 :   0 : 0x00
00001000 :   8 : 0x08
---------------------
10011111 : 159 : 0x9F

所以我們有9F 如果您確定正確的答案是B1 ,則應檢查數據或/和公式

編輯2:如果初始string看起來像(請參閱注釋)

 00$00$04$20$15$8e$00$08$b3$8d$00$08$b5$8d$00$08

我們可以實現GetCheckSum

 // Now we're working with item_1$Item_2$...$Item_N
 public static string GetCheckSum(string value) {
   // TODO: Validate string here

   return value
     .Split('$')
     .Select(item => Convert.ToInt64(item, 16)) // 16 is required in this format
     .Aggregate((s, item) => s ^ item)
     .ToString("X");
 }

 ...

 string test = "00$00$04$20$15$8e$00$08$b3$8d$00$08$b5$8d$00$08";

 // Let's have a look on the the array
 Console.WriteLine(string.Join(", ", test
                   .Split('$')
                   .Select(item => Convert.ToInt64(item, 16))));

 Console.Wrire(GetCheckSum(test));

結果:

 0, 0, 4, 32, 21, 142, 0, 8, 179, 141, 0, 8, 181, 141, 0, 8
 B1  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM