0

I have something like this in c#

        byte a;
        byte b;
        byte c;

        c = a + b;

and it gives an error for c = a + b and says "Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?). I don't understand why because everything is in bytes

Matlab is involved because I am translating an image processing program from matlab into c# where i take values from a picture which are uint8 and doing calculations with that value when it does it the unit8 takes over and during any calculations any number higher than 255 is set to 255. So in c# I just made all of my variables bytes since they are all under 255 anyways but just like in the example code when running the calculations the error pops up.

DebareDaDauntless
  • 471
  • 2
  • 7
  • 12

2 Answers2

1

The arithmetic expression on the right-hand side of the assignment operator evaluates to int by default.

See byte - MSDN

The following assignment statement will produce a compilation error, because the arithmetic expression on the right-hand side of the assignment operator evaluates to int by default.

byte x = 10, y = 20;
byte z = x + y;   // Error: conversion from int to byte

By adding an explicit cast the error will go like:

byte z = (byte)(x + y);   
Habib
  • 219,104
  • 29
  • 407
  • 436
  • as the link provided said, I actually did cast try to cast them before, but the calculation i get in c# is not the same as i get in matlab – DebareDaDauntless Jul 11 '13 at 18:04
0

During any calculations any number higher than 255 is set to 255.

This is not supported natively in C#. Instead, the default behaviour of the (byte) cast is to take the least significant byte, giving an arithmetic result equivalent to modulo 256.

c = unchecked((byte)(200 + 200));

The result above will be 144, which is equivalent to 400 % 256.

If you want to clip results at 255, you need to specify this explicitly:

c = (byte)Math.Min(a + b, 255);
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • if I have a long string of calculations do I have to cast each of them to bytes so instead of (byte)(a+b)*(c+d) it should be (byte)(a+b)*(byte)(c+d) – DebareDaDauntless Jul 11 '13 at 18:10
  • @user2521432: I assume you want to cast at the end: `(byte)((a+b)*(c+d))`. – Douglas Jul 11 '13 at 18:14
  • but is it going to do that max operayion at the end of the calculaiton or if i have a bunch of smaller calculations on one line is it going to clip each of them at 255 as well? because that's what its doing in matlab – DebareDaDauntless Jul 11 '13 at 18:14
  • @user2521432: The `Min` operation will be applied wherever you explicitly insert it. For the example you gave, it doesn't make a difference, but if you had subtractions and divisions, you would need to use it at *every* computation to get results consistent with what you described. For example, `200+200-100` would have to be expressed as `Math.Min(Math.Min(200+200,255)-100,255)`, which will give `155`. – Douglas Jul 11 '13 at 18:17