0
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.

eye_am_groot
  • 682
  • 6
  • 19
  • If you did `var x = 0x09;` what type does it tell you `x` is? – A Friend Dec 18 '18 at 16:46
  • The exclusive OR creates an integer so you must case back to a byte byte[] xor = { (byte)(0x09 ^ 0x45 ^ 0x3a ^ 0x08 ^ cetvrtadvaa) }; – jdweng Dec 18 '18 at 16:47
  • This is by design, `byte ^ byte` returns `int` and needs to be cast back to `byte`. See [C# XOR on two byte variables will not compile without a cast](https://stackoverflow.com/q/2726920). – dbc Dec 18 '18 at 16:49

1 Answers1

0

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) };
jalgames
  • 781
  • 4
  • 23
  • Even if all those literals were bytes instead, the cast would be necessary, because the result of the byte xor operator is an int. – phoog Dec 18 '18 at 16:57