Reputation:
I am getting error in this C# code from 2nd & 3rd line
byte Data = 0x00;
Data = Data | 0x80;
Data = Data >> 1;
Compiler says : Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)
But I successfully run this code in C taking "Data" as unsigned char.
HOW TO DO THIS IN C#?
Thank You for Help.
Upvotes: 0
Views: 449
Reputation:
To prevent the conversion to int, you can use assignment operators instead
byte Data = 0x00;
Data |= 0x80;
Data >>= 1; // result is 0x40
Upvotes: 2
Reputation: 4833
Cast it explicitly like:
byte Data = 0x00;
Data = (byte)(Data | 0x80);
Data = (byte)(Data >> 1);
or declare date as int and convert to byte at the end (if you are sure it fits into byte)
int Data = 0x00;
Data = Data | 0x80;
Data = Data >> 1;
byte bData = (byte)Data;
Upvotes: 1