diff --git a/src/base/base.h b/src/base/base.h index 7263d4b..cbcb3cf 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -10,6 +10,7 @@ extern "C" { #include "optime.h" #include "type_system/type_system.h" #include "../kprint/kprintf.h" +#include "endian.h" #if __cplusplus } diff --git a/src/base/endian.c b/src/base/endian.c new file mode 100644 index 0000000..b6e25f6 --- /dev/null +++ b/src/base/endian.c @@ -0,0 +1,13 @@ +#include "endian.h" + +static const union +{ + uint16 number; + Endian bytes[2]; +} _endian_union={ .number=0x0102 }; + +Endian getEndian(){ + // if 0x0102 == { 1, 2 } then BigEndian + // if 0x0102 == { 2, 1 } then LittleEndian + return _endian_union.bytes[1]; +} diff --git a/src/base/endian.h b/src/base/endian.h new file mode 100644 index 0000000..9d9d1e7 --- /dev/null +++ b/src/base/endian.h @@ -0,0 +1,9 @@ +#include "std.h" + +PACK_ENUM(Endian, + UnknownEndian=0, + LittleEndian=1, + BigEndian=2 +); + +Endian getEndian(); diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 2d62307..156bc37 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -83,18 +83,27 @@ char* toString_float(float64 n, bool withPostfix, bool uppercase){ return cptr_copy(""); } -char* toString_bin(void* _bytes, uint32 size, bool withPrefix){ +char* toString_bin(void* _bytes, uint32 size, bool inverse, bool withPrefix){ char* bytes=_bytes; char* str=malloc(size*8 + (withPrefix?2:0) +1); - uint32 cn=0; + uint32 cn=0; // char number if(withPrefix){ str[cn++]='0'; str[cn++]='b'; } - for(uint32 bn=0; bn>i); + if(inverse){ + for(int32 bn=size-1; bn>=0; bn--){ // byte number + unsigned char byte=bytes[bn]; + for(uint8 i=0; i<8; i++) + str[cn++]='0' + (byte & (char)128>>i); + } + } + else{ + for(uint32 bn=0; bn>i); + } } str[cn]=0; return str; @@ -115,18 +124,29 @@ char _4bitsHex(uint8 u, bool uppercase){ } } -char* toString_hex(void* _bytes, uint32 size, bool withPrefix, bool uppercase){ +char* toString_hex(void* _bytes, uint32 size, bool inverse, bool withPrefix, bool uppercase){ char* bytes=_bytes; char* str=malloc(size*2 + (withPrefix?2:0) + 1); - uint32 cn=0; + uint32 cn=0; // char number if(withPrefix){ str[cn++]='0'; str[cn++]='x'; } - for(uint32 bn=0; bn=0; bn--){ // byte number + unsigned char byte=bytes[bn]; + str[cn++]=_4bitsHex(byte/16, uppercase); + str[cn++]=_4bitsHex(byte%16, uppercase); + } + } + // right to left + else { + for(int32 bn=0; bn