Convert directly from hex to signed short

R

rbtmaxwell

Hello,

Not sure If I'm posting in the correct group here, so I appologize in
advance if I selected the wrong one.

I am trying to initialize a short with a hex value... sounds trivial
right? Well I have not found a way to do this in c# without the
compiler complaining or throwing a exception when its executing the
code.

The only tricky thing here is that the hex number is acutally a
negative number. I can convert ALL of the positive numbers just fine
as you would expect, I have do nothing other than
Convert.ToInt16(<hexnumber>); (assuming hex number is in range)

Without getting into boring specifics of why its delivered to me this
way, utlimatly what I am trying to do is get the C# equivalent to
the C++ code:

short x = 0x8000;

In C++ This will produce a value of -32768 (which is what I would
expect), In C# it will not compile and when properly formatted still
yields a overflow exception. Here is what I tried:

short x = Convert.ToInt16(0x8000); (results in overflow exception)

Or even

short x = Convert.ToInt16(0xFFFF8000); (results in overflow exception,
reason I used 0xFFFF8000 is because it was the value of Int16.MinValue
in hex according to the autos in VS.NET)


I can only believe I have to be doing something wrong here...it has to
be something simple no?
 
P

Pete

Hello,
Without getting into boring specifics of why its delivered to me this
way, utlimatly what I am trying to do is get the C# equivalent to
the C++ code:

short x = 0x8000;

In C++ This will produce a value of -32768 (which is what I would
expect), In C# it will not compile and when properly formatted still
yields a overflow exception. Here is what I tried:

short x = Convert.ToInt16(0x8000); (results in overflow exception)

Try "ushort" or uint16 as your type, you need to use an unsigned type to
contain negatives.
 
M

Mattias Sjögren

I can only believe I have to be doing something wrong here...it has to
be something simple no?

If you have it as a literal in your code you can do

short s = unchecked((short)0x8000);

If you get it as a hex formatted string you do

s = unchecked((short)Convert.ToUInt16("8000", 16));


Mattias
 
J

Jon Skeet [C# MVP]

Pete said:
Try "ushort" or uint16 as your type, you need to use an unsigned type to
contain negatives.

Um, no - unsigned types *can't* contain negatives.

Mattias' solution of making the conversion unchecked is the right one.
 
R

rbtmaxwell

If you have it as a literal in your code you can do

shorts = unchecked((short)0x8000);

If you get it as ahexformatted string you do

s = unchecked((short)Convert.ToUInt16("8000", 16));

Mattias

Ah yes it worked...!!! Thanks so much It was driving me nuts.

On a side note I did get following below to work...though it makes no
sense why the side step worked...but it did. Your way is exactly what
I was looking for (and will be using)
(assume input is unsigned short 0x8000, and output is short)

int temp = Convert.ToInt32(( input | 0xFFFF0000 ) >> 8);
output = Convert.ToInt16((temp << 8) + (input & 0x00FF));
return output;

Thanks to all for the help.
 
J

Jon Skeet [C# MVP]

On a side note I did get following below to work...though it makes no
sense why the side step worked...but it did. Your way is exactly what
I was looking for (and will be using)
(assume input is unsigned short 0x8000, and output is short)

int temp = Convert.ToInt32(( input | 0xFFFF0000 ) >> 8);
output = Convert.ToInt16((temp << 8) + (input & 0x00FF));
return output;

If you take out the expression (temp << 8) + (input & 0x00FF) and print
that out, you'll find it's already -32768, so is within the bounds of a
short, unlike 32768 which isn't.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top