Click here to Skip to main content
Sign Up to vote bad
good
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 - 22:12
Edited 14 Nov '12 - 23:09


2 solutions

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  
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
Your Filters
Interested
Ignored
     
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 173
2 Abhinav S 168
3 Guirec Le Bars 120
4 Ron Beyer 100
0 Sergey Alexandrovich Kryukov 8,439
1 OriginalGriff 6,681
2 CPallini 3,553
3 Rohan Leuva 2,793
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid