This question already has answers here:
C# XOR on two byte variables will not compile without a cast [duplicate]
(7 answers)
Closed 4 years ago.
string cetvrtadva = textBox76.Text.Substring(12, 2);
byte cetvrtadvaa = byte.Parse(cetvrtadva,
System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] xor = { 0x09 ^ 0x45 ^ 0x3a ^ 0x08 ^ cetvrtadvaa };
Why i cant add byte to byte array?
Error: cannot implicitly convert int to byte.
The issue is that you are not putting a byte into the array, but an int.
It is true that cetvrtadvaa is a byte, but all the other numbers (0x09, 0x45, etc.) you use in the xor operation are ints. Therefore, before ^ is actually done, cetvrtadvaa is converted into an int so that both sides of the operation have the same type.
Therefore, you need to explicitly cast the the result back:
byte[] xor = { (byte)(0x09 ^ 0x45 ^ 0x3a ^ 0x08 ^ cetvrtadvaa) };
Related
This question already has answers here:
problem converting 4-bytes array to float in C#
(3 answers)
Closed 9 months ago.
I have a byte array of four bytes which contains the byts of a FLOAT values.
For example
array[0]=0x1F
array[1]=0x05
array[2]=0x01
array[3]=0x42
this should be 0x4201051f, which means 32.255 value.
Do you have any suggestion on how to group the array and save the value in a float data?
Thank you
As suggested by shingo, the linked question provides the answer:
var input = new byte[] { 0x1F, 0x05, 0x01, 0x42 };
Console.WriteLine(BitConverter.ToSingle(input, 0)); // Outputs 32.255
I would like to ask a question about string convert to byte from windows form, i have try several ways to do these, while convert string to hex is successful turn to string but the problem i need to turn it back to hex byte because the API only get bytes.
Here is the convert below:
string getTxtString = txtString.text;
int convertToInt = int32.Parse(getTxtString);
string hexString = convertToInt.toString("X");
// i have try with X2 it will get two digit for example 0A
How to convert to Hex byte in situation like this or please provide other solution.
For example:
11 = 0A
0A is the conversion of below:
int convertToInt = int32.Parse(getTxtString);
string hexString = convertToInt.toString("X2");
From the convert above will only get 0A.
The Api need to whole Hex value like 0x0A, i need to send 0x0A to API.
Try Convert.ToByte while providing fromBase (16 in your case)
// "0A" (string) -> 0x0A == 10 == 0b00001010 (byte)
byte result = Convert.ToByte(hexString, 16);
...
SomeApiMethod(..., result, ...);
In case you have a Int32 encoded in fact (e.g. "FF120A") and you want to get the last byte:
// "FF120A" (or "0xFF120A") -> 0xFF120A (int) -> 0x0A (byte)
// unchecked - we don't want OverflowException on integer overflow
byte result = unchecked((byte)Convert.ToInt32(hexString, 16));
...
SomeApiMethod(..., result, ...);
Please, notice that the byte (e.g. 0x0A) is always the same, it its string representation that can vary:
// 00001010 - binary
Console.WriteLine(Convert.ToString(result, 2).PadLeft(8, '0'));
// 10 - decimal
Console.WriteLine(result);
// 0A - hexadecimal
Console.WriteLine(result.ToString("X2"));
I have a byte array with hexadecimal values, for example:
var b = new byte[] {0x27, 0x01, 0x00, 0x00};
I need to convert this to decimal value, but when I used code below, get unexpected result. Expected is 295, but result is 654376960.
if (BitConverter.IsLittleEndian) Array.Reverse(b);
//int myInt = b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
int value = BitConverter.ToInt32(b, 0);
What's wrong?
Basically your understanding of endianness is wrong - your example is in little-endian format already, so you should only reverse it if BitConverter expects a big-endian format. You just need to invert your condition:
if (!BitConverter.IsLittleEndian) Array.Reverse(b);
(I'd personally put the body of the if statement in braces and new lines, but that's a different matter.)
This question already has answers here:
A clear, layman's explanation of the difference between | and || in c#?
(11 answers)
Closed 8 years ago.
I need to take two bytes and OR them together, a rather simple task. However, I do not know the proper syntax to OR the two bytes together.
byte First = 0x03;
byte Second = 0x15;
//Need to or Them
byte Result = First || Second; //This syntax does not work in C#
You need | Operator
byte Result = (byte)(First | Second);
|| is a logical or not bitwise so you need:
byte Result = (byte) (First | Second);
This question already has answers here:
Why does BinaryReader.ReadUInt32() reverse the bit pattern?
(6 answers)
Closed 9 years ago.
I am trying to read a binary file in C#, but I am facing a problem.
I declared the following:
public static readonly UInt32 NUMBER = 0XCAFEBABE;
Then while reading from the very beginning of the file I am asking to read the first 4 bytes (already tried different ways, but this is the simplest):
UInt32 num = in_.ReadUInt32(); // in_ is a BinaryReader
While I have that the 4 bytes are CA, FE, BA and BE (in hex) while convert them to UInt I am getting different values. NUMBER is 3405691582, num is 3199925962.
I also tried to do this:
byte[] f2 = {0xCA, 0xFE, 0xBA, 0xBE};
and the result of doing BitConverter.ToUInt32(new byte[]{0xCA, 0xFE, 0xBA, 0xBE},0) is 3199925962.
can anyone help me?
This is because of the little endianness of your machine. See BitConverter.IsLittleEndian property to check this.
Basically, numbers are stored in reverse byte order, compared to how you would write them down. We write the most significant number on the left, but the (little endian) PC stores the least significant byte on the left. Thus, the result you're getting is really 0xBEBAFECA (3199925962 decimal) and not what you expected.
You can convert using bit shifting operations:
uint value = (f2[0] << 24) | (f2[1] << 16) | (f2[2] << 8) | f2[3];
There are many more ways to convert, including IPAddress.NetworkToHostOrder as I4V pointed out, f2.Reverse(), etc.
For your specific code, I believe this would be most practical:
uint num = (uint)IPAddress.NetworkToHostOrder(in_.ReadInt32());
This may result in an arithmetic underflow however, so it may cause problems with a /checked compiler option or checked keyword (neither are very common).
If you want to deal with these situations and get even cleaner code, wrap it in an extension method:
public static uint ReadUInt32NetworkOrder(this BinaryReader reader)
{
unchecked
{
return (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32());
}
}
That's what is called byte order:
var result1 = BitConverter.ToUInt32(new byte[] { 0xCA, 0xFE, 0xBA, 0xBE }, 0);
//3199925962
var result2 = BitConverter.ToUInt32(new byte[] { 0xBE, 0xBA, 0xFE, 0xCA }, 0);
//3405691582