Solved

convert signed int to unsigned int

Posted on 2006-06-28
8
2,991 Views
Last Modified: 2010-08-05
Hey Experts,
How do I convert a number represented as a string from a text box to unsigned. For example, the number in the text box is -492961707.
0
Comment
Question by:freename
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 17004924
Do you want to convert it to 492961707 or the unsigned equivalent (base-2 inverse)?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17004931

int n = int.Parse(textbox.text)
uint u = unchecked(n < 0 ? -n : n);
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17004994
Depending on if you want the 32-bit equivalent or 64-bit equivalent,

try
            Int64 i = -492961707;
            String s = i.ToString("x");
            u = UInt64.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);

or

            Int32 i = -492961707;
            String s = i.ToString("x");
            u = UInt32.Parse(s, System.Globalization.NumberStyles.AllowHexSpecifier);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:dstanley9
ID: 17005014
Angel,

Very slick!  But wouldn't it just be

int n = int.Parse(textbox.text)
uint u = (uint)unchecked(n);
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 17007835
dstanley don't be too impressed it is googled :) from http://www.flounder.com/csharpfactoids.htm

Generally one should use Math.Abs ... there is 1 number it will fail for -2147483648 (due to overflow) which is why the unchecked solution posted is mentioned ... if this one number doesn't matter to you then it is a much cleaner answer and does a better job expressing what you are trying to do.

int n = int.Parse(textbox.text)
uint foo = (uint) math.abs(value)


Cheers,

Greg
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17007878
>don't be too impressed it is googled
I admit to be culprit on that point :-)
0
 

Author Comment

by:freename
ID: 17008738
Everyone,
I see some very good solutions here. But I don't think I was clear enough on my original request. My apologies. From what I understand (I'm getting this information second hand),  the number originates on a system that uses unsigned numbers. Then it is delivered (the means is unimportant) to a system that has signed numbers. Therefore, when represented on the new system, the negative sign may be present, as in this case, because the signed bit is set. I would like to know what the original number was on the system the uses unsigned numbers. Is this possible?
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 100 total points
ID: 17009439
It depends on how many bits were used to represent the number on the original system.  The negation of an unsigned int will be different for each:

uint ui = (uint)unchecked(i);  // returns 3802005589 (0xe29e0055)
ulong ul = (ulong)unchecked(i);  // returns 18446744073216589909  (0xffffffffe29e0055)

If the code represents some type of 32-bit structure with a combination of flags and numbers, it is probably a 32-bit code based on your example.  COM errors, for example, are translated to negative 32-bit signed integers becuase the leftmost bit is always 1 for errors to indicate a failure (http://windowssdk.msdn.microsoft.com/en-us/library/97e68708-eb62-4481-af03-cf8b80304103.aspx).

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 46
locate sql commands in C# visual studio Project 6 45
parse string in c# 5 31
Nested forach loop to linq 3 30
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question