user12205558
user12205558

Reputation:

Uncleared Answer of Hexadecimal to decimal conversion

The conversion i wrote of hexadecimal to decimal number is working but get some little bit unexpected result.

Expected result : HEX number "0000000F" and Output is in Decimal "15"

In my case

1] if "0000000F" is HEX value and it's conversion is "1532"

2] if "000000FF" is HEX value and it's conversion is "25532"

I am not sure what part of programming is wrong ? every time I get "32" After any decimal value result. Can Anyone suggest how to fix this issue ?

#include <iostream>
using namespace std ;
#include <sstream>

int wmain() {
while(1)
{
int binNumber ;
unsigned int decimal;
string hexString = "0000000F"; //you may or may not add 0x before
stringstream myStream;
myStream <<hex <<hexString;
myStream >>binNumber;
cout <<binNumber <<decimal;
//return 0;
}
}

Upvotes: 0

Views: 179

Answers (2)

user12205558
user12205558

Reputation:

// Correct code  
 #include <iostream>
 using namespace std ;
 #include <sstream>

int wmain() {
while(1)
{
 int binNumber ;
 // unsigned int dummy=0;
  string hexString = "000000FF"; //you may or may not add 0x before
 stringstream myStream;
 myStream <<hex <<hexString;
 myStream >>binNumber;
 cout <<binNumber ;
  //return 0;
}
}

Upvotes: 0

molbdnilo
molbdnilo

Reputation: 66459

If you look at the first parts of your outputs, you will notice that they are correct.

But here:

cout <<binNumber <<decimal;

you're printing decimal immediately afterwards, and it is uninitialised.
Remove it.

(On a related note, don't declare variables that you think you might need at some point in the future. Many bugs lurk down that way.)

Upvotes: 1

Related Questions