mirror of
https://onedev.site.tesses.net/tesses-framework
synced 2026-03-25 22:40:20 +00:00
Add signed to bitconverter and float
This commit is contained in:
@@ -10,42 +10,34 @@ namespace Tesses::Framework::Serialization
|
||||
*/
|
||||
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 float ToFloatBits(uint32_t v);
|
||||
|
||||
static uint32_t ToUint32Bits(float v);
|
||||
|
||||
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 float ToFloatBE(uint8_t& b);
|
||||
|
||||
static uint64_t ToUint64BE(uint8_t& b);
|
||||
static uint32_t ToUint32BE(uint8_t& b);
|
||||
static uint16_t ToUint16BE(uint8_t& b);
|
||||
|
||||
static double ToDoubleLE(uint8_t& b);
|
||||
static float ToFloatLE(uint8_t& b);
|
||||
static uint64_t ToUint64LE(uint8_t& b);
|
||||
static uint32_t ToUint32LE(uint8_t& b);
|
||||
static uint16_t ToUint16LE(uint8_t& b);
|
||||
|
||||
static int64_t ToSint64BE(uint8_t& b);
|
||||
static int32_t ToSint32BE(uint8_t& b);
|
||||
static int16_t ToSint16BE(uint8_t& b);
|
||||
static int64_t ToSint64LE(uint8_t& b);
|
||||
static int32_t ToSint32LE(uint8_t& b);
|
||||
static int16_t ToSint16LE(uint8_t& b);
|
||||
|
||||
static Uuid ToUuid(uint8_t& b);
|
||||
|
||||
|
||||
@@ -53,14 +45,25 @@ class BitConverter {
|
||||
|
||||
|
||||
static void FromDoubleBE(uint8_t& b, double v);
|
||||
static void FromFloatBE(uint8_t& b, float 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);
|
||||
|
||||
static void FromDoubleLE(uint8_t& b, double v);
|
||||
static void FromFloatLE(uint8_t& b, float v);
|
||||
static void FromUint64LE(uint8_t& b, uint64_t v);
|
||||
static void FromUint32LE(uint8_t& b, uint32_t v);
|
||||
static void FromUint16LE(uint8_t& b, uint16_t v);
|
||||
|
||||
static void FromSint64BE(uint8_t& b, int64_t v);
|
||||
static void FromSint32BE(uint8_t& b, int32_t v);
|
||||
static void FromSint16BE(uint8_t& b, int16_t v);
|
||||
|
||||
static void FromSint64LE(uint8_t& b, int64_t v);
|
||||
static void FromSint32LE(uint8_t& b, int32_t v);
|
||||
static void FromSint16LE(uint8_t& b, int16_t v);
|
||||
|
||||
static void FromUuid(uint8_t& b, const Uuid& uuid);
|
||||
|
||||
|
||||
|
||||
@@ -4,16 +4,40 @@ namespace Tesses::Framework::Serialization
|
||||
{
|
||||
double BitConverter::ToDoubleBits(uint64_t v)
|
||||
{
|
||||
return *(double*)&v;
|
||||
static_assert(sizeof(double) == sizeof(uint64_t), "double is not the same size as uint64_t");
|
||||
double dest=0;
|
||||
memcpy(&dest,&v, sizeof(uint64_t));
|
||||
return dest;
|
||||
}
|
||||
uint64_t BitConverter::ToUintBits(double v)
|
||||
{
|
||||
return *(uint64_t*)&v;
|
||||
//as static_assert is compile time we don't need it here
|
||||
uint64_t dest = 0;
|
||||
memcpy(&dest,&v, sizeof(uint64_t));
|
||||
return dest;
|
||||
}
|
||||
float BitConverter::ToFloatBits(uint32_t v)
|
||||
{
|
||||
static_assert(sizeof(float) == sizeof(uint32_t), "float is not the same size as uint32_t");
|
||||
float dest=0;
|
||||
memcpy(&dest,&v, sizeof(uint32_t));
|
||||
return dest;
|
||||
}
|
||||
uint32_t BitConverter::ToUint32Bits(float v)
|
||||
{
|
||||
//as static_assert is compile time we don't need it here
|
||||
uint32_t dest = 0;
|
||||
memcpy(&dest,&v, sizeof(uint32_t));
|
||||
return dest;
|
||||
}
|
||||
double BitConverter::ToDoubleBE(uint8_t& b)
|
||||
{
|
||||
return ToDoubleBits(ToUint64BE(b));
|
||||
}
|
||||
float BitConverter::ToFloatBE(uint8_t& b)
|
||||
{
|
||||
return ToFloatBits(ToUint32BE(b));
|
||||
}
|
||||
uint64_t BitConverter::ToUint64BE(uint8_t& b)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
@@ -52,6 +76,10 @@ namespace Tesses::Framework::Serialization
|
||||
double BitConverter::ToDoubleLE(uint8_t& b)
|
||||
{
|
||||
return ToDoubleBits(ToUint64LE(b));
|
||||
}
|
||||
float BitConverter::ToFloatLE(uint8_t& b)
|
||||
{
|
||||
return ToFloatBits(ToUint32LE(b));
|
||||
}
|
||||
uint64_t BitConverter::ToUint64LE(uint8_t& b)
|
||||
{
|
||||
@@ -126,6 +154,10 @@ namespace Tesses::Framework::Serialization
|
||||
{
|
||||
FromUint64BE(b,ToUintBits(v));
|
||||
}
|
||||
void BitConverter::FromFloatLE(uint8_t& b, float v)
|
||||
{
|
||||
FromUint32BE(b,ToUint32Bits(v));
|
||||
}
|
||||
void BitConverter::FromUint64LE(uint8_t& b, uint64_t v)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
@@ -195,5 +227,52 @@ namespace Tesses::Framework::Serialization
|
||||
|
||||
}
|
||||
|
||||
int64_t BitConverter::ToSint64BE(uint8_t& b)
|
||||
{
|
||||
uint64_t src = ToUint64BE(b);
|
||||
int64_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint64_t));
|
||||
return dest;
|
||||
}
|
||||
int64_t BitConverter::ToSint64LE(uint8_t& b)
|
||||
{
|
||||
uint64_t src = ToUint64LE(b);
|
||||
int64_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint64_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
int32_t BitConverter::ToSint32BE(uint8_t& b)
|
||||
{
|
||||
uint32_t src = ToUint32BE(b);
|
||||
int32_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint32_t));
|
||||
return dest;
|
||||
}
|
||||
int32_t BitConverter::ToSint32LE(uint8_t& b)
|
||||
{
|
||||
uint32_t src = ToUint32LE(b);
|
||||
int32_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint32_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
int16_t BitConverter::ToSint16BE(uint8_t& b)
|
||||
{
|
||||
uint16_t src = ToUint16BE(b);
|
||||
int16_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint16_t));
|
||||
return dest;
|
||||
}
|
||||
int16_t BitConverter::ToSint16LE(uint8_t& b)
|
||||
{
|
||||
uint16_t src = ToUint16LE(b);
|
||||
int16_t dest = 0;
|
||||
memcpy(&dest,&src,sizeof(uint16_t));
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user