Click here to Skip to main content
12,624,887 members (36,242 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi all,
When converting a string value to integer value the result return as hexadecimal format.
Ex: int output=Convert.ToInt16("1");
when watch output value in a add watch it displayed as 0x001.

and int output1 = Convert.ToInt32("1");
output1 value displayed as 0x00000001.
I don't the problem. But this problem occurs only in my system. Please reply me if u know the answer.
Posted 14-Nov-12 23:12pm
Updated 15-Nov-12 0:09am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

No, it doesn't.

Integers do not have a "Hex format", or a "decimal format" - they are numbers (and are in fact always stored in binary).

The only time you use a hexadecimal format is when you are converting from a sting in hex format ro an integer, or from an integer to a string in hex format. In the first case, you would use:
int myInteger = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
and in the second:
string hexString = myInteger.ToString("X4");
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The input string has to represent the number in decimal for Convert.ToInt16(String)[^] to work.
Its output is an integer. That has neither decimal nor hexadecimal format. It's just the correct value.
You can convert it into a string again to display it to the user. Use the ToString() method[^] for that.
If you want the output string to be in hexadecimal format, use
Int16 number = 255;
string output = number.ToString("X");
for that.

If you want to convert a string that represents a number in hexadecimal form to an integer, use
Int16 number;
bool success = Int16.TryParse(
    "0xFF",
    NumberStyles.HexNumber,
    CultureInfo.InvariantCulture,
    out value
);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161128.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100