473,385 Members | 1,409 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.

casting from short to ushort

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

Sep 19 '06 #1
4 10904
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));

"ra***********@gmail.com" wrote:
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

Sep 19 '06 #2
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.

Sep 19 '06 #3
ra***********@gmail.com wrote:
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

Sep 19 '06 #4
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.

"ra***********@gmail.com" wrote:
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.

Sep 19 '06 #5

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

Similar topics

1
by: Marco [Stinger] | last post by:
COMPACT FRAMEWORK Hi Newbe question: I've got an external (Embedded C+) function that reads a PLC and returns an array of 16 unsigned (PLC people call them WORD, but in C# thay should be...
4
by: kelli | last post by:
i am new to c# so if this is a trivial problem, forgive me! i've searched the web and after 2 days still cannot solve it. i am converting a c++ application to c# - the problem code is listed...
2
by: AMP | last post by:
Hello, I have: ushort length= 4; ushort len; And I get an error here: length = len + 4; Error 6 Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (are you...
6
by: AMP | last post by:
Hello, First, Thanks for ALL your help. Second, another question: I have some c code i am turning into c#:(truncated) WORD calcChecksum(BYTE data, WORD length) /* Calculates a checksum of...
1
by: dast | last post by:
I would need a function to copy unsigned short values from a IntPtr to a ushort-array, but the Marshal.Copy-function only support the short-array! At the moment I do it the following way: ......
12
by: O.B. | last post by:
I'm trying to do a static_cast at runtime in C# and as I understand it, "as" is the keyword to use. Unfortunately, the compiler is trying to do the cast a compilation time. See below. /*...
1
by: xlar54 | last post by:
I have some code that ends up looking alot like: ushort x = (ushort)(m + b); whereas both m and b are ushorts also. This is due to the automatic casting of values to int. Some quick...
22
by: Yakov | last post by:
What would be the nicest way to write the loop for all values of unsigned short (0..0xffff), usnig 'unsigned short port;' as an index ? For comparison, there is just one way to write the for(k=0;...
1
by: Deanm007 | last post by:
How is this code giving the correct answer? I though unsigned short int is only positive numbers. I am giving it a -3 and I am getting 0 for sum. thanks #include <iostream> using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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.