Removing unnecessary lines and correcting the output of binary numbers
Этот коммит содержится в:
@@ -1,5 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -13,8 +14,7 @@ char nibble_to_hex(uint8_t i) //Conversion to hex format
|
||||
void print_in_hex(uint8_t byte)
|
||||
{
|
||||
cout << nibble_to_hex(byte >> 4)
|
||||
<< nibble_to_hex(byte & 0xf)
|
||||
<< endl;
|
||||
<< nibble_to_hex(byte & 0xf);
|
||||
}
|
||||
|
||||
const uint8_t* as_bytes(const void* data);
|
||||
@@ -51,13 +51,6 @@ bit_digit(uint8_t byte, uint8_t bit) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// ... (îñòàëüíûå ôóíêöèè èç ïðåäûäóùåãî êîäà îñòàþòñÿ áåç èçìåíåíèé)
|
||||
|
||||
void print_in_binary(uint8_t byte) {
|
||||
for (int bit = 7; bit >= 0; bit--) {
|
||||
@@ -67,21 +60,26 @@ void print_in_binary(uint8_t byte) {
|
||||
|
||||
void print_in_binary(const void* data, size_t size) {
|
||||
const uint8_t* bytes = static_cast<const uint8_t*>(data);
|
||||
|
||||
// Ïå÷àòàåì áàéòû â îáðàòíîì ïîðÿäêå äëÿ ïðàâèëüíîãî îòîáðàæåíèÿ ÷èñåë
|
||||
for (int i = size - 1; i >= 0; i--) {
|
||||
print_in_binary(bytes[i]);
|
||||
|
||||
// Ðàçäåëèòåëè äëÿ óäîáñòâà ÷òåíèÿ
|
||||
if (i > 0) {
|
||||
if (i % 4 == 0)
|
||||
cout << "\n";
|
||||
else
|
||||
cout << " ";
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
// Äîáàâëÿåì ïðîáåë ïåðåä áàéòîì, êðîìå ïåðâîãî â ñòðîêå
|
||||
if (i % 4 == 0 && i != 0) {
|
||||
cout << "\n";
|
||||
}
|
||||
else if (i != 0) {
|
||||
cout << " ";
|
||||
}
|
||||
print_in_binary(bytes[i]);
|
||||
}
|
||||
cout << "\n"; // Ïåðåíîñ ñòðîêè â êîíöå
|
||||
}
|
||||
|
||||
struct Example {
|
||||
uint8_t a;
|
||||
uint16_t b;
|
||||
uint32_t c;
|
||||
char d[3];
|
||||
};
|
||||
|
||||
int main() {
|
||||
// Òåñòèðîâàíèå ÷èñåë èç ëåêöèîííîãî ñëàéäà
|
||||
uint16_t a = 0x4d2; // 1234 â äåñÿòè÷íîé
|
||||
@@ -112,6 +110,16 @@ int main() {
|
||||
print_in_binary(&xorr, sizeof(xorr));
|
||||
cout << "\n\n";
|
||||
|
||||
Example arr[2] = {
|
||||
{0x01, 0x0203, 0x04050607, {'a', 'b', 'c'}},
|
||||
{0x08, 0x090a, 0x0b0c0d0e, {'d', 'e', 'f'}}
|
||||
};
|
||||
|
||||
cout << "The location of the structure in memory (hex):\n";
|
||||
print_in_hex(arr, sizeof(arr));
|
||||
cout << "\n\nThe location of the structure in memory (binary):\n";
|
||||
print_in_binary(arr, sizeof(arr));
|
||||
|
||||
|
||||
cin.ignore();
|
||||
return 0;
|
||||
|
||||
Ссылка в новой задаче
Block a user