Hello
How do I combine hexadecimal numbers? For example if I have the numbers:
- aa
- 14
- 5d
How can I combine them to get : aa145d ?
How do those hexadecimal numbers store? do they store as integer or string?
Get the input as a string and append those strings :
cout<<"Enter a hex number : ";
string s1, s2;
cin >> s1 >> s2;
s1 += s2;
cout<<s1;
How do those hexadecimal numbers store? do they store as integer or string?
I have 3 integer variables & I want to combine them into one integer variable and as I said before each variable is an hexadecimal number
#include <iostream>
using namespace std;
int main() {
cout << hex;
cin >> hex;
int a = aa;
int b = 14;
int c = 5d;
// This does not work
int combination = (a&b&c);
// I am trying to achieve the result where..
// the variable combination = aa145d
return 0;
}
There are a few things wrong with your code: int a = aa;
. The compiler will complain about not knowing what 'aa' is. If you want to input hex-numbers, you need to add a 0x, so : int a = 0xaa;
.
This: int combination = (a&b&c);
is wrong. Do you know how the & operator works? You should write this on paper and see why it doesn't work. This is what you're doing:
10101010 (0x0aa)
00010100 (0x14)
01011101 (0x5d)
-------------- &
00000000 = 0x00
What you need to do is shift the number x places and then 'or' them which eachother.
int main() {
int a = 0xaa;
int b = 0x14;
int c = 0x5d;
int combination = (a << 16) | (b << 8) | c;
cout << hex << combination;
return 0;
}
hi
i also have similar issue to solve
instead of 4 byte i have 8 byte of data
char a = 0x00;
char b = 0x00;
char c = 0x00;
char d = 0x00;
char e = 0x00;
char f = 0xbc;
char g = 0x61;
char h = 0x4e;
long long int combination = (a << 56) |(b << 48) | (c << 40) | (d << 32) |
(e << 24) | (f << 16) | (g << 8) | (h);
but i got warning as
warning C4293: '<<' : shift count negative or too big, undefined behavior
and result is not proper..
suggest something..
> warning C4293: '<<' : shift count negative or too big, undefined behavior
56, 48, 40 are greater than the number of bits in an int on the implementation you are using (which seems to have a 32 bit int). Convert (or cast) to a 64 bit integral type first. Also, prefer using bit-wise shift operations on unsigned integral types.
unsigned char a = 0x00;
unsigned char b = 0x00;
unsigned char c = 0x00;
unsigned char d = 0x00;
unsigned char e = 0x00;
unsigned char f = 0xbc;
unsigned char g = 0x61;
unsigned char h = 0x4e;
unsigned long long aa = a ;
unsigned long long bb = b ;
unsigned long long cc = c ;
unsigned long long dd = d ;
unsigned long long int combination = (aa << 56) |(bb << 48) | (cc << 40) | (dd << 32) |
(e << 24) | (f << 16) | (g << 8) | (h);
Hi vijayan121,
thanks a lot
now it is working fine
:)
:)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.