473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Convert directly from hex to signed short

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?

Feb 20 '07 #1
5 12424

<rb********@gmail.comwrote in message
news:11**********************@t69g2000cwt.googlegr oups.com...
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.
Feb 20 '07 #2
>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.
Feb 21 '07 #3
Pete <me@ineedhelp.orgwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 21 '07 #4
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.


Feb 21 '07 #5
<rb********@gmail.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Alan | last post by:
how to convert double to short ? for example, I want to convert double doubleVal1 = 15000.1; double doubleVal2 = 12000.0; short shortVal; shortVal = doubleVal1 - doubleVal2; I...
3
by: Omer kamal | last post by:
Dear all, I want to save my data into MS Access database Table which consist of some fields with short date and short time formats. I try to save data in to the said table and I am getting error...
9
by: Igor Okulist | last post by:
int func(void**); { short* p = NULL; func(&p); //<<< here } Could somebody remind me why is this not allowed ? error message: "cannot convert parameter from 'short **' to 'void **'"
2
by: aqmaiya | last post by:
Hello, there is string value 'Dec 06, 2000' I want to convert that string date to SHORT FORMAT like '2000-12-06-. Please help me how do I do that? I'm new in Jython. Thanks, aqmaiya
2
by: Bo Berglund | last post by:
I am trying to port code from Delphi to C++ as described in a previous post titled: "How to swap bytes in variables (VS2005)???" Now I have run into a bit of a snag due to my ignorance of C++, no...
3
eyalbi007
by: eyalbi007 | last post by:
Hi all, I need to perfom simple-looking task in C#: Converting byte array to short array - meaning that if my byte array has 100 cells, my short array will have 50 cells. If it was C/C++ it...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.