Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

int crc_register;
byte[] MCUData = new byte[200];
MCUData[frame_length] = crc_register >> 8;
frame_length++;
MCUData[frame_length] = crc_register & 0xFF;
frame_length++;

What I have tried:

Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?
Posted
Updated 16-Jan-19 22:46pm

:sigh:
You aren't listening, are you? When you asked this question: Cannot implicitly convert type 'int' to 'bool'[^] I said:
Quote:
When you find code on the internet in one language and try to use it in another, you have to understand three things:
1) The source language - in this case C or C++
2) The destination language - in this case C#
3) What the code does, and how it does it.
And that is exactly what I meant.
You need to understand both languages; you can't just dump C code into C# and expect it to compile! Worse, even if you get it to compile cleanly, there is no guarantee at all that it will work in exactly the same way as the original code did, these are different languages which share some common syntax.

So think about what is going on, rather than just blindly trying to get others to translate it for you...
In this case, you are trying to put an integer value into a byte variable, and C# won't let you "just do that" because the operation "throws away" information and it want's to be sure you know that is what you are doing.
Change this:
MCUData[frame_length] = crc_register >> 8;
And all similar code to this:
MCUData[frame_length] = (byte)(crc_register >> 8);
and this error message will disappear.

You still need to be aware that you cannot expect the code even when it compiles to work exactly the same as the original unless you comply with all three of the conditions above!
 
Share this answer
 
Quote:
int crc_register;
byte[] MCUData = new byte[200];
MCUData[frame_length] = crc_register >> 8;
frame_length++;
MCUData[frame_length] = crc_register & 0xFF;
frame_length++;

As an addition to Griff's solution, are you aware you are using the uninitialized value of crc_register?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900