User Tools

Site Tools

blog:2023-03-16_c_hex_string_table_conver_to_byte_array



2023-03-16 C#: HEX String Table Conver into Byte Array

  • I have a hex string table as follows, need to convert it into the binary array.
  • 000000  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    000010  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    000020  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    000030  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    000040  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    000050  FF  2D  2D  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . - - . . . . . . . . . . . . .
    000060  02  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    000070  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    000080  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    000090  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    0000A0  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    0000B0  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    0000C0  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    0000D0  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    0000E0  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
    0000F0  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF  FF   . . . . . . . . . . . . . . . .
  • This string is stored in VAR_ProcessParseOutputString.

Solution

  • private static void Task_ParseEepromContents()
    {
      int _PositionIndex = 0;
      string _SubStr = string.Empty;
      string _str;
      // 
      if (string.IsNullOrEmpty(VAR_ProcessParseOutputString)) return;
      for (int _idx = 0; _idx < 16; _idx++)
      {
        // 建立尋找之index字串
        _str = (_idx * 16).ToString("X06");
        // 找到搜尋字串的起始位置
        _PositionIndex = VAR_ProcessParseOutputString.IndexOf(_str);
        // 裁剪含有HEX Value之子字串,並去除所有之空白字元
        _SubStr = (VAR_ProcessParseOutputString.Substring(_PositionIndex + 8, 62)).Replace(" ", "");
        //_SubStr = (VAR_ProcessParseOutputString.Substring(_PositionIndex + 8, 62)).Trim();
        for (int _idx2 = 0; _idx2 < _SubStr.Length; _idx2 += 2)
        {
          // 對每一個子字串,每2個字元切出來,由字元轉換成HEX Value存於byte陣列中
          // 每一字串中含有16個HEX Vaule,所以應該有32個字元
          VAR_LevelingSensorEeprom[_idx * 16 + _idx2 / 2] =
            (byte)Convert.ToByte(_SubStr.Substring(_idx2, 2), 16);
        }
        // Console.WriteLine(_str + "," + _SubStr); // 這是驗證用的Code
      }
    }

TAGS

  • 90 person(s) visited this page until now.

Permalink blog/2023-03-16_c_hex_string_table_conver_to_byte_array.txt · Last modified: 2023/03/16 16:59 by jethro

oeffentlich