mirror of
https://onedev.site.tesses.net/tesses-framework
synced 2026-02-08 07:45:46 +00:00
Move bitconverter from crosslang to tessesframework
This commit is contained in:
@@ -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
|
||||
|
||||
48
include/TessesFramework/Serialization/BitConverter.hpp
Normal file
48
include/TessesFramework/Serialization/BitConverter.hpp
Normal file
@@ -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);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -37,3 +37,4 @@
|
||||
#include "Platform/Environment.hpp"
|
||||
#include "Platform/Process.hpp"
|
||||
#include "Text/StringConverter.hpp"
|
||||
#include "Serialization/BitConverter.hpp"
|
||||
85
src/Serialization/BitConverter.cpp
Normal file
85
src/Serialization/BitConverter.cpp
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user