diff options
Diffstat (limited to 'ZAPDUtils/Utils/BinaryWriter.cpp')
| -rw-r--r-- | ZAPDUtils/Utils/BinaryWriter.cpp | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/ZAPDUtils/Utils/BinaryWriter.cpp b/ZAPDUtils/Utils/BinaryWriter.cpp index c456bde..aa7840f 100644 --- a/ZAPDUtils/Utils/BinaryWriter.cpp +++ b/ZAPDUtils/Utils/BinaryWriter.cpp @@ -10,6 +10,11 @@ BinaryWriter::BinaryWriter(std::shared_ptr<Stream> nStream) stream = nStream; } +void BinaryWriter::SetEndianness(Endianness endianness) +{ + this->endianness = endianness; +} + void BinaryWriter::Close() { stream->Close(); @@ -47,16 +52,25 @@ void BinaryWriter::Write(uint8_t value) void BinaryWriter::Write(int16_t value) { + if (endianness != Endianness::Native) + value = BSWAP16(value); + stream->Write((char*)&value, sizeof(int16_t)); } void BinaryWriter::Write(uint16_t value) { + if (endianness != Endianness::Native) + value = BSWAP16(value); + stream->Write((char*)&value, sizeof(uint16_t)); } void BinaryWriter::Write(int32_t value) { + if (endianness != Endianness::Native) + value = BSWAP32(value); + stream->Write((char*)&value, sizeof(int32_t)); } @@ -68,33 +82,48 @@ void BinaryWriter::Write(int32_t valueA, int32_t valueB) void BinaryWriter::Write(uint32_t value) { + if (endianness != Endianness::Native) + value = BSWAP32(value); + stream->Write((char*)&value, sizeof(uint32_t)); } void BinaryWriter::Write(int64_t value) { + if (endianness != Endianness::Native) + value = BSWAP64(value); + stream->Write((char*)&value, sizeof(int64_t)); } void BinaryWriter::Write(uint64_t value) { + if (endianness != Endianness::Native) + value = BSWAP64(value); + stream->Write((char*)&value, sizeof(uint64_t)); } void BinaryWriter::Write(float value) { + if (endianness != Endianness::Native) + value = BitConverter::ToFloatBE((uint8_t*)&value, 0); + stream->Write((char*)&value, sizeof(float)); } void BinaryWriter::Write(double value) { + if (endianness != Endianness::Native) + value = BitConverter::ToDoubleBE((uint8_t*)&value, 0); + stream->Write((char*)&value, sizeof(double)); } void BinaryWriter::Write(const std::string& str) { int strLen = str.size(); - stream->Write((char*)&strLen, sizeof(int)); + Write(strLen); for (char c : str) stream->WriteByte(c); |
