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;
}
@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;
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
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.
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.
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;
@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.
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;
}