6 Replies - 12419 Views - Last Post: 10 July 2011 - 03:39 AM Rate Topic: -----

#1 Banksy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 11

Cannot implicitly convert type 'int' to 'byte'.

Posted 10 July 2011 - 01:59 AM

Alright, so my friend made a custom encryption in VB.NET and I wanted to convert it to C# and this is what I got.

public byte[] PolyRevDeCrypt(byte[] data, string pass)
        {
            Array.Reverse(data);
            byte rndByte = data[data.Length - 1];
            byte[] passByte = System.Text.Encoding.Default.GetBytes(pass);
            byte[] otu = new byte[data.Length + 1];
            int u = 0;
            int i;
            for (i = 0; i <= data.Length - 1; i++)
            {
                otu[i] = (data[i] ^ rndByte) ^ passByte[u];
                Array.Reverse(passByte);
                if (u == passByte.Length - 1)
                    u = 0;
                else
                    u += 1;
            }
            return otu;
        }


The error comes from.

otu[i] = (data[i] ^ rndByte) ^ passByte[u];


And the error is.

Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot implicitly convert type 'int' to 'byte'.

#2 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Cannot implicitly convert type 'int' to 'byte'.

Posted 10 July 2011 - 02:41 AM

Hi,

The problem is that your trying to implicitly fit an int variable into a byte variable.

The ^ operator, in this case, returns an integer. As casting an integer to a byte is a narrowing conversion, you need to explicitly cast the result of the XOR to a byte type before putting it in the 'otu[i]' variable.

This post has been edited by [email protected]: 10 July 2011 - 04:05 AM

Was This Post Helpful? 2
  • +
  • -

#3 Banksy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 11

Re: Cannot implicitly convert type 'int' to 'byte'.

Posted 10 July 2011 - 03:01 AM

This is what I got (it's wrong, probably by a long shot)

public byte[] PolyRevDeCrypt(byte[] data, string pass)
        {
            Array.Reverse(data);
            byte rndByte = data[data.Length - 1];
            byte[] passByte = System.Text.Encoding.Default.GetBytes(pass);
            byte[] otu = new byte[data.Length + 1];
            int u = 0;
            int i;
            for (i = 0; i <= data.Length - 1; i++)
            {
                int[] x = (byte[])data;
                int hi = (byte)rndByte;
                int[] ha = (byte[])passByte;
                otu[i] = (x[i] ^ hi) ^ ha[u];
                Array.Reverse(passByte);
                if (u == passByte.Length - 1)
                    u = 0;
                else
                    u += 1;
            }
            return otu;
        }


What am I doing wrong now?
Was This Post Helpful? 0
  • +
  • -

#4 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Cannot implicitly convert type 'int' to 'byte'.

Posted 10 July 2011 - 03:11 AM

Your casting bytes and byte arrays to bytes and byte arrays. That's not really necessary ;).

All you actually need is something like this:

otu[i] = (byte)((data[i] ^ rndByte) ^ passByte[u]);



Think about it this way. Both the byte and int types are unsigned integral types. So conversions between them are definitely possible.

A byte is an 8 bit type; an int is a 32 bit type.

Therefore, as 8 bits can easily fit into 32 bits, a byte variable can implicitly be converted to an int type without you having to do anything.

As the ^ operator only acts on operands of type bool or int by default, the byte variables in your XOR expression are implicitly converted to int type. No problem there.

However, the result of the XOR is an int, of which you are then trying to store in a byte variable; 'otu[i]'.

Unfortunately, 32-bits cannot be implicitly squeezed into 8 bits. Therefore, you need to force the int to fit in the byte variable, using an explicit cast. The result is that the 24 excess high order bits are dropped to allow the int to be fitted into the 8-bit byte variable :)

This post has been edited by [email protected]: 10 July 2011 - 04:10 AM

Was This Post Helpful? 1
  • +
  • -

#5 Banksy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 11

Re: Cannot implicitly convert type 'int' to 'byte'.

Posted 10 July 2011 - 03:19 AM

Alright, is this correct? It doesn't show any errors, but just to be sure. :)
public byte[] PolyRevDeCrypt(byte[] data, string pass)
        {
            Array.Reverse(data);
            byte rndByte = data[data.Length - 1];
            byte[] passByte = System.Text.Encoding.Default.GetBytes(pass);
            byte[] otu = new byte[data.Length + 1];
            int u = 0;
            int i;
            for (i = 0; i <= data.Length - 1; i++)
            {
                int x = (byte)data[i];
                int hi = (byte)rndByte;
                int ha = (byte)passByte[u];
                byte xam = (byte)((x ^ hi) ^ ha);
                otu[i] = xam;
                Array.Reverse(passByte);
                if (u == passByte.Length - 1)
                    u = 0;
                else
                    u += 1;
            }
            return otu;
        }

Was This Post Helpful? 0
  • +
  • -

#6 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: Cannot implicitly convert type 'int' to 'byte'.

Posted 10 July 2011 - 03:38 AM

Yes, that will work, but your still casting bytes to bytes, which is obviously uneccessary. All you needed to change in the code you posted in your first post was this line:

otu[i] = (data[i] ^ rndByte) ^ passByte[u];


to

otu[i] = (byte)((data[i] ^ rndByte) ^ passByte[u]);


But yes, your code should compile as it is :)

This post has been edited by [email protected]: 10 July 2011 - 03:43 AM

Was This Post Helpful? 1
  • +
  • -

#7 Banksy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-July 11

Re: Cannot implicitly convert type 'int' to 'byte'.

Posted 10 July 2011 - 03:39 AM

Oh never mind, saw that part of your post. Thanks! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1