Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

conversion from a byte to a signed byte

Posted on 2013-06-20
6
Medium Priority
?
425 Views
Last Modified: 2013-06-20
hi - i'm using C#, VS2010 and .net 4. I have a line of code --

mesDataDisplay_WFG.graph.PlotYAppend((double)(Convert.ToSByte(HIDData[0])), xIncrement);

where the HIDData is typed as a byte array. I get an overflow exception saying that the value '254' is an overflow. I would have expected it to convert automatically to -2. it's not that I can't do the arithmetic to make it work, but I don't understand why it doesn't make the conversion.
thanks.
0
Comment
Question by:jdcoburn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39264494
There is no such thing as a "signed" byte. Valid byte values are 0 to 255.

Screenshot
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39264528
This is not quite correct:
http://msdn.microsoft.com/en-us/library/a7yfs6t2.aspx

Convert.ToSByte(Byte)

"Converts the value of the specified 8-bit unsigned integer to the equivalent 8-bit signed integer."

Throws OverflowException if value is greater than SByte.MaxValue (The value of this constant is 127; that is, hexadecimal 0x7F).
See http://msdn.microsoft.com/en-us/library/system.sbyte.maxvalue.aspx.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 39264544
Try:

            byte b = 254;
            sbyte sb = unchecked((sbyte)b);
            System.Diagnostics.Debug.WriteLine("byte = {0}; sbyte = {1}", b, sb);

Open in new window


Output:

byte = 254; sbyte = -2
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 83

Expert Comment

by:David Johnson, CD, MVP
ID: 39264560
static void Main(string[] args)
        {
            UInt16[] numbers = { 127, 128, 65535, 65534 };
            Int16 output;
            Int16 counter;
            for (counter = 0; counter < 4; counter++)
            {
                output = unchecked((Int16)numbers[counter]);
                Console.WriteLine("Output = {0}", output);
            }
            Console.ReadLine();
        }

gives me the results
Output = 127
Output = 128
Output = -1
Output = -2

I run on a 64bit computer therefor int8 is not available.  If I change it to
Int16[] numbers = { 127, 128, 65535, 65534 };
the last 2 values are greater than maxint16
0
 

Author Closing Comment

by:jdcoburn
ID: 39264837
well done - this is exactly what I needed. the first comment was not only wrong, it was snooty.
thanks,
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39264856
I concede that it was incorrect--in terms of no sbyte, not in terms of the valid values for byte, but it wasn't intended to be snooty. (I'm honestly not certain what part of my statement could be considered "snooty.") I was thinking about the type incorrectly. Every other integral type in .NET that is signed is simply its name (e.g. Int16, Int32, Int64), and the unsigned equivalents all start with a "U" (e.g. UInt16, UInt32, UInt64). It was simply a flawed projection by me that you were stuck with byte since there was no "UByte". I completely overlooked that there was an sbyte.
0

Featured Post

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question