PC Review


Reply
Thread Tools Rate Thread

Convert directly from hex to signed short

 
 
rbtmaxwell@gmail.com
Guest
Posts: n/a
 
      20th Feb 2007
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?

 
Reply With Quote
 
 
 
 
Pete
Guest
Posts: n/a
 
      20th Feb 2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      21st Feb 2007
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Feb 2007
Pete <(E-Mail Removed)> wrote:
> > 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.


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

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

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
rbtmaxwell@gmail.com
Guest
Posts: n/a
 
      21st Feb 2007
On Feb 21, 12:28 am, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:
> >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
>
> shorts = unchecked((short)0x8000);
>
> If you get it as ahexformatted string you do
>
> s = unchecked((short)Convert.ToUInt16("8000", 16));
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
> Please reply only to the newsgroup.


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.








 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Feb 2007
<(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am trying to convert a signed packed field in Excel. Is there . =?Utf-8?B?cmRmcmVk?= Microsoft Excel Worksheet Functions 3 16th Nov 2007 02:49 PM
how do i convert a byte[2] with format big endian signed int to a integer value? eywitteveen@gmail.com Microsoft C# .NET 6 5th Oct 2007 04:46 PM
How do i convert a byte[2] with format big endian signed int to a integer value? eywitteveen@gmail.com Microsoft C# .NET 3 30th Sep 2007 09:11 PM
how to convert DateTime in short date and short time Omer kamal Microsoft C# .NET 3 13th Sep 2005 12:07 AM
Can I convert directly from excel to RTF? =?Utf-8?B?RWZhbnN0YXk=?= Microsoft Excel Misc 2 14th Feb 2005 06:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:35 PM.