HEX to DEC

Mar 7, 2012 at 7:42pm
closed account (4w7X92yv)
Hello
Does someone knows a good HEX to DEC converted written in c++?
I wrote one but it only works if user inputs a code and not when a code is programmed. (it has to work like:
int hex = ff; ... output is 255;
whitout having to insert a input

Best Regards
Joriek

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
int hex = 0;

std::cin >> std::hex >> hex;

std::cout << hex;
std::cout << std::endl;
system ("pause");
return 0;
}
Mar 7, 2012 at 8:06pm
1
2
int x = 0xff;
std::cout << x;
The key here is the "0x" before the hex.
Last edited on Mar 7, 2012 at 8:06pm
Mar 7, 2012 at 8:15pm
closed account (4w7X92yv)
thanks :)
Mar 7, 2012 at 8:21pm
closed account (4w7X92yv)
@L B
now if i have a int called Number (witch contains a HEX code 11223344)
how can i do it so it reads it as 0x11223344?
Number is recieved from other little programs

thanks

1
2
3
4
int number = 11223344;
int hex = 0xNumber;

std::cout << hex;
Mar 7, 2012 at 8:28pm
No, the 0x thing only applies to text that you type as numbers, aka constants. It does not work for variable names, even if they are const.

Whether you use hex or decimal, it is all binary in the computer's memory - so you just need to specify how you want to interpret it:
1
2
3
4
5
int numA = 0xFF;
int numB = 255;

std::cout << numA << ' ' << numB << std::endl;
std::cout << std::hex << numA << ' ' << std::hex << numB << std::endl;
255 255
ff ff
Last edited on Mar 7, 2012 at 8:28pm
Mar 7, 2012 at 8:37pm
closed account (4w7X92yv)
Is there no other way to convert that Number to dec? or to bin?
Mar 7, 2012 at 8:51pm
There is no 'conversion', you're just interpreting the number differently when converting it to a string.

std::dec
std::hex
std::oct

http://www.cplusplus.com/reference/iostream/ see Manipulators.

There is not one for binary, but that should not be difficult for you.
Last edited on Mar 7, 2012 at 8:51pm
Mar 7, 2012 at 9:01pm
closed account (4w7X92yv)
but how can i interprete the variable Number as a binary code?
idk how to do that
i know, my knowledge of c++ if very limited
but i beg you, can you help me further
thanks
Mar 7, 2012 at 9:03pm
Whenever you write a number, the compiler just reads it as binary. Using a cout will just format it as decimal, scientific, hex, oct, etc.

If you write 71 the compiler will store this as: 0100 0111. <-- decimal
If you write 0x47 the compiler will store this as: 0100 0111. <-- hex
If you write 0107 the compiler will store this as: 0100 0111. <-- octal
If you write 'G' the compiler will store this as: 0100 0111. <-- ascii

Now just output it however you like.

If you write int num = ff; you'll get a compilation error that ff is not defined. You need to write this as int num = 0xff.
Last edited on Mar 7, 2012 at 9:06pm
Mar 7, 2012 at 9:04pm
std::bitset is fine for binary.
1
2
3
char c = 0xFF;
std::bitset<8> char_bs(c); // templated argument is the size in bits
std::cout << bs.to_string() << std::endl;
Mar 7, 2012 at 9:10pm
closed account (4w7X92yv)
@Texan40
bs undeclared
how do i solve?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <bitset>

int main()
{

char c = 0xFF;
std::bitset<8> char_bs(c);
std::cout << bs.to_string() << std::endl;

system ("pause");
return 0;

}
Mar 7, 2012 at 9:10pm
Wait, are you saying that you want to output the binary value with cout? I've never seen std::bitset as mentioned above but whenever I want to look at stuff in binary, I just use hex. It's more professional and much easier to read than binary as you don't have to do as much counting. Just learn to count by 4s.
Mar 7, 2012 at 9:11pm
closed account (4w7X92yv)
@Stewbond
i have to output the value as binary, so i can convert it to dec whit a IEEE754 single precision floating piont 32bit
Last edited on Mar 7, 2012 at 9:12pm
Mar 7, 2012 at 9:14pm
You could write your own function if you like...

1
2
3
4
5
6
7
8
9
10
11
std::string Num2Bin(int num)
{
    std::string output;
    while(num/1 > 0)
    {
        if (num%2 == 1) output += '1';
        else            output += '0';
        num /= 2;
    }
    return output;
}


Edit: forgot to return the string
Last edited on Mar 7, 2012 at 9:29pm
Mar 7, 2012 at 9:19pm
closed account (4w7X92yv)
1 problem: input is 50, output is 010011 but it should be 110010, how can i change this?
and i guess its a dec to bin, it should be hex to bin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <bitset>

int main()
{

int num;   
std::cin >> num;

std::string Num2Bin(int num);
{
    std::string output;
    while(num/1 > 0)
    {
        if (num%2 == 1) output += '1';
        else            output += '0';
        num /= 2;
    }
    std::cout << output;
    std::cout << std::endl;
}

system ("pause");
return 0;

}
Last edited on Mar 7, 2012 at 9:21pm
Mar 7, 2012 at 9:23pm
Whoa, you're not just outputing it, you are planning to operate on it? Then don't bother converting it to binary. It's already in binary in your compiler.

If you are manually writing bits, the following will be your best friends:

Getting a bit:
1
2
3
4
5
int num = ...;
bit1 = num & 0x00000001;
bit2 = num & 0x00000002;
bit7 = num & 0x00000040;
etc...


Setting a bit:
1
2
3
4
num = (bit1 & 0x1) << 0; // the 0x1 isn't nessesary, but safe
num = (bit2 & 0x1) << 1;
num = (bit7 & 0x1) << 7;
etc...


<< is the left bit-shift operator
>> is the right bit-shift operator
& is a bit-wise and
| is a bit-wise or
^ is a bit-wise xor

You can do anything you want with these.

Another example, if you want to extract the third byte of a value:
byte3 = (num & 0x00ff0000) >> 16;
If you want to ensure the 20th bit is 1:
num |= 0x00080000;
If you want to ensure the 23rd bit is 0:
num &= 0x00400000;

I hope you see the pattern and the power
Last edited on Mar 7, 2012 at 9:28pm
Mar 7, 2012 at 9:24pm
Regarding the function above... my bad..., it's inversed. Just reverse the bits.
Mar 7, 2012 at 9:26pm
closed account (4w7X92yv)
@Stewbond
Tomorrow ill post the things i need, the program that i already have and what still has to happen
Then ill hope someone comes whit a converter from HEX to BIN or from HEX to DEC.

Mar 7, 2012 at 9:33pm
Here this inverses the bits for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::string Num2Bin(int num)
{
    std::string binary;
    while(num/1 > 0)
    {
        if (num%2 == 1) output += '1';
        else            output += '0';
        num /= 2;
    }

    // Fill the output with ANYTHING for now (just to allocate memory to it)
    std::string output;
    output.reserve((int)(log(num)/log(2))+1);

    // Inverse the bits
    for (int i = 0; i < binary.size(); i++)
        output[i] = binary[binary.size()-(i+1)];

    return output;
}


Now just use it like this:
1
2
3
4
cout << Num2Bin(25) << endl;
cout << Num2Bin(0x75) << endl;
cout << Num2Bin(032) << endl;
cout << Num2Bin('A') << endl;
11001
1110101
11010
1000001
Last edited on Mar 7, 2012 at 9:39pm
Mar 7, 2012 at 9:35pm
Sorry, I'm just sloppy and impatient sometimes. My code should have been:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <bitset>

int main()
{
  char c = 0xFF;
  std::bitset<8> char_bs(c);
  std::cout << char_bs.to_string() << std::endl;
  return 0;
}
Topic archived. No new replies allowed.