From 36b050fc08373836381413e61bc0469034800e0c Mon Sep 17 00:00:00 2001 From: Mike Nolan Date: Tue, 23 Sep 2025 20:29:29 -0500 Subject: [PATCH] Move bitconverter from crosslang to tessesframework --- CMakeLists.txt | 1 + .../Serialization/BitConverter.hpp | 48 +++++++++++ include/TessesFramework/TessesFramework.hpp | 1 + src/Serialization/BitConverter.cpp | 85 +++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 include/TessesFramework/Serialization/BitConverter.hpp create mode 100644 src/Serialization/BitConverter.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 64721e9..858590c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ src/Http/WebSocket.cpp src/Mail/Smtp.cpp src/Serialization/Json.cpp src/Serialization/SQLite.cpp +src/Serialization/BitConverter.cpp src/Platform/Environment.cpp src/Platform/Process.cpp src/Streams/FileStream.cpp diff --git a/include/TessesFramework/Serialization/BitConverter.hpp b/include/TessesFramework/Serialization/BitConverter.hpp new file mode 100644 index 0000000..af8b411 --- /dev/null +++ b/include/TessesFramework/Serialization/BitConverter.hpp @@ -0,0 +1,48 @@ +#pragma once +#include "../Common.hpp" +namespace Tesses::Framework::Serialization +{ + +/** + * @brief A bit converter + * + */ +class BitConverter { + public: + /** + * @brief Get the bits of a double from a int64_t + * + * @param v a int64_t with double's bits + * @return double the double + */ + static double ToDoubleBits(uint64_t v); + /** + * @brief Get the bits of a int64_t from a double + * + * @param v a double with int64_t's bits + * @return uint64_t the int64_t + */ + static uint64_t ToUintBits(double v); + /** + * @brief Get big endian double from uint8_t reference of first element of 8 byte array + * + * @param b a reference to the first byte of an array + * @return double the double + */ + static double ToDoubleBE(uint8_t& b); + /** + * @brief Get big endian uint64_t from uint8_t reference of first element of 8 byte array + * + * @param b a reference to the first byte of an array + * @return uint64_t the uint64_t + */ + static uint64_t ToUint64BE(uint8_t& b); + static uint32_t ToUint32BE(uint8_t& b); + static uint16_t ToUint16BE(uint8_t& b); + static void FromDoubleBE(uint8_t& b, double v); + static void FromUint64BE(uint8_t& b, uint64_t v); + static void FromUint32BE(uint8_t& b, uint32_t v); + static void FromUint16BE(uint8_t& b, uint16_t v); +}; + +} \ No newline at end of file diff --git a/include/TessesFramework/TessesFramework.hpp b/include/TessesFramework/TessesFramework.hpp index d36a997..974cbdf 100644 --- a/include/TessesFramework/TessesFramework.hpp +++ b/include/TessesFramework/TessesFramework.hpp @@ -37,3 +37,4 @@ #include "Platform/Environment.hpp" #include "Platform/Process.hpp" #include "Text/StringConverter.hpp" +#include "Serialization/BitConverter.hpp" \ No newline at end of file diff --git a/src/Serialization/BitConverter.cpp b/src/Serialization/BitConverter.cpp new file mode 100644 index 0000000..72651a7 --- /dev/null +++ b/src/Serialization/BitConverter.cpp @@ -0,0 +1,85 @@ +#include "TessesFramework/Serialization/BitConverter.hpp" + +namespace Tesses::Framework::Serialization +{ + double BitConverter::ToDoubleBits(uint64_t v) + { + return *(double*)&v; + } + uint64_t BitConverter::ToUintBits(double v) + { + return *(uint64_t*)&v; + } + double BitConverter::ToDoubleBE(uint8_t& b) + { + return ToDoubleBits(ToUint64BE(b)); + } + uint64_t BitConverter::ToUint64BE(uint8_t& b) + { + uint8_t* b2 = &b; + uint64_t v = 0; + v |= ((uint64_t)b2[0] << 56); + v |= ((uint64_t)b2[1] << 48); + v |= ((uint64_t)b2[2] << 40); + v |= ((uint64_t)b2[3] << 32); + v |= ((uint64_t)b2[4] << 24); + v |= ((uint64_t)b2[5] << 16); + v |= ((uint64_t)b2[6] << 8); + v |= (uint64_t)b2[7]; + return v; + } + uint32_t BitConverter::ToUint32BE(uint8_t& b) + { + uint8_t* b2 = &b; + uint32_t v = 0; + + v |= ((uint32_t)b2[0] << 24); + v |= ((uint32_t)b2[1] << 16); + v |= ((uint32_t)b2[2] << 8); + v |= (uint32_t)b2[3]; + return v; + } + uint16_t BitConverter::ToUint16BE(uint8_t& b) + { + uint8_t* b2 = &b; + uint16_t v = 0; + + + v |= ((uint16_t)b2[0] << 8); + v |= (uint16_t)b2[1]; + return v; + } + void BitConverter::FromDoubleBE(uint8_t& b, double v) + { + FromUint64BE(b,ToUintBits(v)); + } + void BitConverter::FromUint64BE(uint8_t& b, uint64_t v) + { + uint8_t* b2 = &b; + b2[0] = (uint8_t)(v >> 56); + b2[1] = (uint8_t)(v >> 48); + b2[2] = (uint8_t)(v >> 40); + b2[3] = (uint8_t)(v >> 32); + b2[4] = (uint8_t)(v >> 24); + b2[5] = (uint8_t)(v >> 16); + b2[6] = (uint8_t)(v >> 8); + b2[7] = (uint8_t)v; + } + void BitConverter::FromUint32BE(uint8_t& b, uint32_t v) + { + uint8_t* b2 = &b; + + b2[0] = (uint8_t)(v >> 24); + b2[1] = (uint8_t)(v >> 16); + b2[2] = (uint8_t)(v >> 8); + b2[3] = (uint8_t)v; + } + void BitConverter::FromUint16BE(uint8_t& b, uint16_t v) + { + uint8_t* b2 = &b; + + b2[0] = (uint8_t)(v >> 8); + b2[1] = (uint8_t)v; + } + +}; \ No newline at end of file