The goal is to get a byte [16], where the first element is the hexadecimal value 55, and the second element is the hexadecimal value AA. And the remaining 14 are the hexadecimal value of 0.
I tried
byte[] outStream = System.Text.Encoding.UTF8.GetBytes("55 AA 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
but it fills byte [] with ascii values, not hexadecimal values.
byte[] outStream = new byte[16]; outStream[0] = byte.Parse("55"); outStream[1] = byte.Parse("AA"); for(int i=2; i<16; i++) { outStream[i] = byte.Parse("00"); }
but it doesnβt work either. It does not give hexadecimal values, but integer values ββthat fail on AA, since it is not a syntax index.
Any help would be appreciated.
You can write a hexadecimal integer literal in C # whose prefix is ββ0x:
byte[] result = new byte[16]; result[0] = 0x55; result[1] = 0xaa;
- 0x00, .
:
byte[] result = new byte[] { 0x55, 0xaa, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
: byte.Parse(hex_byte_string, System.Globalization.NumberStyles.HexNumber);: Convert.ToByte(hex_byte_string, 16);
byte.Parse(hex_byte_string, System.Globalization.NumberStyles.HexNumber);
Convert.ToByte(hex_byte_string, 16);
public static byte[] ToByteArray(String HexString) { string hex_no_spaces = HexString.Replace(" ",""); int NumberChars = hex_no_spaces.Length; byte[] bytes = new byte[NumberChars / 2]; for (int i = 0; i < NumberChars; i+=2) { bytes[i / 2] = byte.Parse(hex_no_spaces.Substring(i, 2), System.Globalization.NumberStyles.HexNumber); } return bytes; }
byte[] bytez = ToByteArray("55 AA 00 00 00 00 00 00 00 00 00 00 00 00 00 00");
byte[] result = { 0x55, 0xaa, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Source: https://habr.com/ru/post/1770403/More articles:A strongly typed generic method calls the base class method of the argument, not the shadow method in T? - genericsThe right way to maintain an advanced multimedia database for Android a la MediaStore? - androidThe best way to transfer data from one database (sql server 2008) to another db (sql server 2008) with another schema is synchronizationHow to access podcast metadata? - androidGet extents from a drawing using a database without opening a drawing - c #How to tell gzip_static not to search for image files? - nginxAccess to class variables? - ruby ββ| fooobar.comWhy is Enum considered safer in type than constants? - javaJavaScript / jQuery and PHP Ajax throw an error - javascriptIs there a way to use my current SSH connection to transfer SCP? - sshAll Articles