Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to convert an int into byte and then comapare that value with another byte value.

But it is throwing exception while converting.

Below is the code.

What I have tried:

start_index = 0;
int sum = 0;

for (int i = 0; i <= totalLength-2; i++)
{
    sum = (sum + outBuffer.ElementAt(start_index));
    start_index++;
}

int value =Convert.ToByte( outBuffer.ElementAt(totalLength - 1));


byte s = Convert.ToByte(sum); //this lines throws an exception
                              //sum can be upto 5000
                    //so i am trying to convert it into byte because my value is in bytes then i wish to compare them

if (sum==value)
{
    ret_pair.Item2.AddFirst(outBuffer.ElementAt(start_index + 2));
    Convert.ToString("Inside the if block");
}
Posted
Updated 25-Jun-18 23:06pm
Comments
F-ES Sitecore 26-Jun-18 4:52am    
You can't convert an int to a byte, an int is big enough for 5000 but a byte isn't, a byte can only be in the rage 0 to 255. It sounds like the problem is with your logic and what you're ultimately trying to do. But you probably need to forget about "s" and use the int sum instead. You should explain why that causes you a problem.
Nishikant Tayade 26-Jun-18 4:59am    
As you said byte can only be in the range 0 to 255.So I did these and now it's working perfectly.

byte[] s = BitConverter.GetBytes(sum);
if (s[0] == value)
{
start_index = 0;
ret_pair.Item2.AddFirst(outBuffer.ElementAt(start_index + 2));
Console.WriteLine("Inside if block");
}
F-ES Sitecore 26-Jun-18 5:01am    
Um, ok, but I don't think that code is doing what you think it is doing. Just because it isn't throwing an error doesn't mean the logic is sound.
Richard MacCutchan 26-Jun-18 5:14am    
That is just so wrong. Don't use bytes to do the compare, use the largest type that you are using to hold any value. In this case the largest type is an int, so use int for both values. Downcasting from int to byte means you will get some values that look equal even when they are not.
Afzaal Ahmad Zeeshan 26-Jun-18 5:14am    
Also, you can check if the value is in the range of the byte values and then convert it.

Just take a look at documentaion: Convert.ToByte Method (Int32) (System)[^] :

OverflowException	

value is less than Byte.MinValue or greater than Byte.MaxValue. 


And at MaxValie [^] :
The value of this constant is 255 (hexadecimal 0xFF).


Nice to know that the Byte is 8 Bits: 2^8 = 256 (from 0 till 255) .
 
Share this answer
 
v3
You don't need Convert to change from an integer to a byte; you can just cast it:
C#
byte s = (byte)sum;
But ... if your sum can be up to 5000, then that will not fit in a byte - which has a fixed range of 0 to 255 inclusive (because it is defined as an 8 bit value, and that is all it can hold).

You need to look at your whole system and work out why you want a byte at all - if the value is in the range 0 to 50900, then your whole system is going to fail if you continue to try an use byte values to hold them.
 
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