casting from short to ushort

R

rakesh.Mysore

HI

I have a problem in casting short to ushort in my project.

i have a ushort value of 38,143 now it is converted into short value of
27,393 and stored in variable.(when i casted ushort to short)

1. now how can i get back value 38,143 from short variable value 27,393
?
2. how can i check whether the conversion of value happned when i
casted
from ushort to short ?

if anybody have idea please let me know, thanks
 
G

Guest

Hi, Quite interested in knowing why you are doing this?To Quote Jesse Liberty
in his book Programming C# : "That said, memory is fairly cheap, and
programmer time is increasingly expensive; most of the time
you'll simply declare your variables to be of type int, unless there is a
good reason to do otherwise.
The signed types are the numeric types of choice of most programmers unless
they have a good
reason to use an unsigned value.
Although you might be tempted to use an unsigned short to double the
positive values of a signed
short (moving the maximum positive value from 32,767 up to 65,535), it is
easier and preferable to
use a signed integer (with a maximum value of 2,147,483,647). "
Try:
ushort uSigned = 65535;
short signed = 0;
signed = (short)uSigned;
Console.WriteLine("Short == {0}" , signed);
ushort newUnSigned = (ushort)signed;
Console.WriteLine("UShort == {0}", newUnSigned);
Console.WriteLine("Values Equal ? : {0}" , (uSigned ==
newUnSigned));
 
R

rakesh.Mysore

Hello

Thanks for reply.
that realy helped me.

Actually iam having a hardware which stores the value in short
data type form, and am using ushort datatype in my application.

am reading data from hardware which is of type short and assign to
ushort.

now i want to get ushort value for overflowed short value.

for example, i have value 38,143 in ushort variable, which is converted
to 27,393 while assiging to short variable. i need to get back 38,143
value from 27,393 stored in short variable.

Is there is any overflow indication which indication overflow occured
in varable value.
 
J

Jon Skeet [C# MVP]

Thanks for reply.
that realy helped me.

Actually iam having a hardware which stores the value in short
data type form, and am using ushort datatype in my application.

am reading data from hardware which is of type short and assign to
ushort.

now i want to get ushort value for overflowed short value.

for example, i have value 38,143 in ushort variable, which is converted
to 27,393 while assiging to short variable.

No, that shouldn't happen. It should be converted to -27393.
i need to get back 38,143 value from 27,393 stored in short variable.

If you got -27393, just a cast would do it.

Here's a sample program showing how it should work:

using System;

class Test
{
static void Main()
{
ushort x = 38143;
short y = (short) x;
Console.WriteLine (y);
ushort z = (ushort) y;
Console.WriteLine (z);
}
}

It sounds like your conversion from ushort to short has issues...

Jon
 
G

Guest

i have value 38,143 in ushort variable, which is converted
to 27,393 while assiging to short variable

How are you doing this?

If the uShort variable has a value greater than 32K then on conversion to a
short value you should get a negative value.
 

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