Coordinate Id of same card that is read by two different card readers

醉酒当歌 提交于 2019-12-04 21:33:11

The answer is that the second reader is returning ASCII encoded decimals. Your number is 1059548974. This number, encoded into hexadecimals is 3F276F2E if you use Big Endian encoding. If you use Little Endian encoding then you will get 2E6F273F which should be familiar to you.

So:

  1. decode the returned byte array to ASCII, retrieving the string "1059548974"
  2. convert that string to an integer using Convert.ToUInt32(str);
  3. reverse the bytes in the integer

Probably the best way to reverse the bytes is this piece of code:

public static UInt32 ReverseBytes(UInt32 value)
{
  return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
         (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
}

Its rather hard to understand just exactly what your wanting, but in the bottom you state: 'in other words, i want to convert this array of bytes to corresponding hex code'.

You can perform that operation like so:

public static string ByteArrayToString(byte[] ba)
{
  StringBuilder sb = new StringBuilder(ba.Length * 2);
  foreach (byte b in ba)
    {
        sb.AppendFormat("{0:x2}", b);
    }
  return sb.ToString();
}

Just pass in your Byte array and the result will be your hex conversion in string format.

Edit: This will probably yield the same results, but try this:

byte[ ] bytes = {0,   1,   2,   4,   8,  16,  32,  64, 128, 255 }
Console.WriteLine( BitConverter.ToString( bytes ) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!