Я получаю данные от другого хоста через сетевой сокет в формате с прямым порядком байтов. Как интерпретировать байты, полученные в собственном формате с порядком байтов (например, получить представление или повторно интерпретировать эти байты) без копирования во временную переменную.
#include
#include
struct A {
uint16_t msg_id;
// contains many other fields where total size is greater than 4096
void print() const {
// print all the fields of struct A
}
};
struct B {
uint16_t msg_id;
// contains many other fields where total size is greater than 4096
void print() const {
// print all the fields of struct B
}
};
struct C {
uint16_t msg_id;
// contains many other fields where total size is greater than 4096
void print() const {
// print all the fields of struct C
}
};
int main() {
char buff[8192];
while (true) {
// data is received in network byte order (big endian) but my system is little endian
const auto recvd_len = recvfrom(sock_fd, buff, sizeof(buff), 0, nullptr, nullptr);
const uint16_t msg_id = (buff[0] print();
// the above print call works correctly only if my system is big endian but not little endian
}
break;
case 0x0002: {
// reinterpret the bytes received as struct B, copy elision
const B* b_obj = reinterpret_cast(buff);
b_obj->print();
// the above print call works correctly only if my system is big endian but not little endian
}
break;
case 0x0003: {
// reinterpret the bytes received as struct C, copy elision
const C* c_obj = reinterpret_cast(buff);
c_obj->print();
// the above print call works correctly only if my system is big endian but not little endian
}
break;
default:
break;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... -endianess
Переинтерпретировать байты буфера в соответствии с системным (родным) порядком байтов ⇐ C++
Программы на C++. Форум разработчиков
1716964634
Anonymous
Я получаю данные от другого хоста через сетевой сокет в формате с прямым порядком байтов. Как интерпретировать байты, полученные в собственном формате с порядком байтов (например, получить представление или повторно интерпретировать эти байты) без копирования во временную переменную.
#include
#include
struct A {
uint16_t msg_id;
// contains many other fields where total size is greater than 4096
void print() const {
// print all the fields of struct A
}
};
struct B {
uint16_t msg_id;
// contains many other fields where total size is greater than 4096
void print() const {
// print all the fields of struct B
}
};
struct C {
uint16_t msg_id;
// contains many other fields where total size is greater than 4096
void print() const {
// print all the fields of struct C
}
};
int main() {
char buff[8192];
while (true) {
// data is received in network byte order (big endian) but my system is little endian
const auto recvd_len = recvfrom(sock_fd, buff, sizeof(buff), 0, nullptr, nullptr);
const uint16_t msg_id = (buff[0] print();
// the above print call works correctly only if my system is big endian but not little endian
}
break;
case 0x0002: {
// reinterpret the bytes received as struct B, copy elision
const B* b_obj = reinterpret_cast(buff);
b_obj->print();
// the above print call works correctly only if my system is big endian but not little endian
}
break;
case 0x0003: {
// reinterpret the bytes received as struct C, copy elision
const C* c_obj = reinterpret_cast(buff);
c_obj->print();
// the above print call works correctly only if my system is big endian but not little endian
}
break;
default:
break;
}
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/78547417/reinterpret-bytes-of-buffer-according-to-systemnative-endianess[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия