Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 30th, 2008, 03:39 AM
Authorized User
Join Date: Aug 2007
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to yukijocelyn
Default Converting int to byte

Hi! I'm having same problems with this:

sendWholeMsg[1] = (byte)msgindex;
sendWholeMsg[2] = (byte)msgindex >> 8;
sendWholeMsg[3] = (byte)msgindex >> 16;
sendWholeMsg[4] = (byte)msgindex >> 24;
The error when I compile is as stated below:
Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)

I don't understand the error here, is there anything wrong with my codes?


Old May 30th, 2008, 03:44 AM
samjudson's Avatar
Friend of Wrox
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts

(byte)msgindex >> 8
The above code is equivalent to the following (if you put in place implicit conversions that are taking place.

((int)(byte)msgindex) >> 8
The >> operators takes two integers, so the cast from msgindex to byte is then being cast again to an integer.

What you are probably trying to do is this:

(byte)(msgindex >> 8)
/- Sam Judson : Wrox Technical Editor -/

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error converting data type varchar to int. - SQL baysaa SQL Server ASP 1 March 16th, 2008 11:56 PM
Converting Int -> ASCII silaros C++ Programming 1 June 5th, 2007 01:12 AM
Converting a String to a Byte array BrianWren Pro Visual Basic 2005 2 February 20th, 2007 11:03 PM
converting image in picturebox to byte[] connect2sandep General .NET 0 July 26th, 2005 04:31 PM
Converting int to string Judex C++ Programming 3 March 1st, 2004 10:44 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.