-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathlittle_big.cpp
More file actions
45 lines (36 loc) · 1.55 KB
/
little_big.cpp
File metadata and controls
45 lines (36 loc) · 1.55 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdexcept>
#include <cstring>
#include <iomanip>
#include <iostream>
void PrintMemory(int value, bool reverse = false) {
unsigned char* bytes = reinterpret_cast<unsigned char*>(&value);
std::cout << "0x";
if (reverse) {
// Вывод в обратном порядке (big-endian)
for (int i = sizeof(int) - 1; i >= 0; --i) {
std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]);
}
} else {
// Вывод в естественном порядке (little-endian)
for (size_t i = 0; i < sizeof(int); ++i) {
std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]);
}
}
std::cout << std::endl;
}
void PrintMemory(double value, bool reverseBytes = false) {
unsigned char* bytes = reinterpret_cast<unsigned char*>(&value);
std::cout << "0x";
if (reverseBytes) {
// Вывод в обратном порядке (big-endian)
for (int i = sizeof(double) - 1; i >= 0; --i) {
std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]);
}
} else {
// Вывод в естественном порядке (little-endian)
for (size_t i = 0; i < sizeof(double); ++i) {
std::cout << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]);
}
}
std::cout << std::endl;
}