I’m working on converting a string into a hexadecimal and then perform & operation. Here is the scenario which seems to have an issue:
byte[] buffer;
string hexoutput;
char[] WaitXMSvalues = WaitXMS.ToCharArray(); // WaitXMS is a textbox, input = 10
foreach (char letter in WaitXMSvalues)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
hexoutput = String.Format("{0:X}", value);
}
buffer[0] = Convert.ToByte(hexoutput & 0xFF);
throws me an error at the above line:
Operator '&' cannot be applied to operands of type 'string' and 'int'
Whats the issue here?
I had done this in my C++ app as follows:
buffer[0] = WaitXMS->getText().getHexValue32() & 0xFF;
and seems to work fine. Whats wrong with my C# code?
Please help!
hexoutputis a string; there is no defined&operation between string and an integer – did you typo in the question? If you are trying to apply a byte-mask, you’ll have to do that when the value is some kind of integer/byte; not as a string.For example, the following would work, but would be somewhat pointless:
There’s also a significant error in that your
hexoutputvariable is defined inside the loop, but accessed outside, again suggesting that the code being shown is not the actual code – that would have the compiler error: