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 ?
Good question. Two different things are getting mixed here: concatenating the text "aa", "14", "5d" (what shows) versus packing three 1-byte values into a single integer value (what you want). was right to ask how you store them. If you want one integer holding the bits 0xAA, 0x14, 0x5D in that order, you need bit packing, not string concatenation and not the & operator (which clears bits). Also prefer unsigned fixed-width types so left-shifts are well-defined, as and hinted.
Here is a small, reusable helper that safely packs N bytes into an unsigned integer in big-endian order (first byte becomes the most significant). It avoids the "shift count too big" warning saw by promoting before shifting.
#include <cstdint>
#include <type_traits>
#include <iomanip>
#include <iostream>
template <class T, class... Bytes>
constexpr T pack_be(Bytes... bs) {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
T v = 0;
for (unsigned b : { static_cast<unsigned>(bs)... }) {
v = static_cast<T>((v << 8) | (static_cast<T>(b) & static_cast<T>(0xFF)));
}
return v;
}
int main() {
std::uint32_t x = pack_be<std::uint32_t>(0xAA, 0x14, 0x5D); // 0x00AA145D
std::cout << std::hex << std::nouppercase
<< std::setw(6) << std::setfill('0') << (x & 0xFFFFFFu) << "\n"; // prints aa145d
} Notes:
int or char, mask with & 0xFF before packing.std::hex (or a 0x prefix) when reading or writing hex.Jump to Post— invisal 381How do those hexadecimal numbers store? do they store as integer or string?
Jump to Post— Nick Evan 4,005There 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 …
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.