1

I want to convert the string value to int value, so I tried these things:

// 1.
string a = "4163";
int b = int.Parse(a);

// 2.
int intValue;
int.TryParse(a, out intValue);

// 3.
int b = Convert.ToInt32(a);

but always my output looks like this:

Wrong output value in debugger

It always writes 0x00001043, but I need 4163 as output. What is the wrong with my code?

Carsten
  • 11,287
  • 7
  • 39
  • 62
aruni
  • 2,664
  • 10
  • 44
  • 68

4 Answers4

8

You code should work as expected. May be you need to change the display settings. So do something like this

Right click the Watch Window and deselect the Hexadecimal Display of values.

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
Sachin
  • 40,216
  • 7
  • 90
  • 102
  • +1, look [here](http://stackoverflow.com/questions/3354453/visual-studio-debugger-displaying-integer-values-in-hex) – Jens Kloster May 28 '13 at 06:33
  • You are copy then past in this answer : http://stackoverflow.com/questions/3354453/visual-studio-debugger-displaying-integer-values-in-hex –  May 28 '13 at 06:49
  • @RameshRams ohh really? – Sachin May 28 '13 at 06:56
  • Yes .. You can see that !!!! this is not a answer. it's like a comment only . and this answer was possible duplicate –  May 28 '13 at 07:13
  • @RameshRams so what do you think what's the correct answer of the OP's problem? – Sachin May 28 '13 at 07:17
  • The OP's have a answer ,for Why he see a watch window ??? –  May 28 '13 at 07:22
0

If you're sure it'll parse correctly, use

int.Parse(string)

If you're not, use

int i;
bool success = int.TryParse(string, out i);

Caution! In this case, i will equal 0, not 10 after the TryParse.

int i = 10;
bool failure = int.TryParse("asdf", out i);

This is because TryParse uses an out parameter, not a ref parameter.

0

you can also use

 int i = Convert.ToInt16("1234"); 

and I think the way you are converting is also correct but you are having problem while u are displaying on the console.

try to display that converted integer in html or somewhere else

Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189
0

Hey Your all converting way's is give correct answer . You don't see a answer in a quick watch. You can check for final value on the b .

for sample

// 1.
string a = "4163";
int b = Convert.ToInt32(a);

the b is return correct answer , you want to check for add below line

int c =b;//now it's show result for  4163