[英]Reading an uint32_t from a byte stream
我正在通過std::basic_ifstream<std::uint8_t>
從PNG圖像讀取字節。 我在讀取應解釋為32位int的4字節序列時遇到問題。
std::uint32_t read_chunk_length(std::basic_ifstream<std::uint8_t> &ifs) {
std::uint32_t length;
ifs.read(reinterpret_cast<std::uint8_t*>(&length), 4);
return length;
}
當讀取的序列為00 00 00 0d且應為0xd(或13)時,上述函數給我0xd000000(或218103808)。 抱歉,這個問題無關緊要。
這是一個字節順序問題 -磁盤上的流包含的字節順序與您的體系結構對整數所要求的順序相反( PNG規范中指定的大字節序)(可能是little-endian)。 您必須手動反轉字節順序才能解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.