Click here to Skip to main content
14,140,660 members
Rate this:
 
Please Sign up or sign in to 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

: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!
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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?
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190518.1 | Last Updated 17 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100