This commit is contained in:
timerix 2023-01-19 20:59:02 +06:00
parent 31c7d4e31b
commit e007c46f0e
6 changed files with 67 additions and 23 deletions

View File

@ -10,6 +10,7 @@ extern "C" {
#include "optime.h"
#include "type_system/type_system.h"
#include "../kprint/kprintf.h"
#include "endian.h"
#if __cplusplus
}

13
src/base/endian.c Normal file
View File

@ -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];
}

9
src/base/endian.h Normal file
View File

@ -0,0 +1,9 @@
#include "std.h"
PACK_ENUM(Endian,
UnknownEndian=0,
LittleEndian=1,
BigEndian=2
);
Endian getEndian();

View File

@ -83,18 +83,27 @@ char* toString_float(float64 n, bool withPostfix, bool uppercase){
return cptr_copy("<float>");
}
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<size; bn++){
unsigned char byte=bytes[bn];
for(uint8 i=0; i<8; i++)
str[cn++]='0' + (byte & (char)128>>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<size; bn++){
unsigned char byte=bytes[bn];
for(uint8 i=0; i<8; i++)
str[cn++]='0' + (byte & (char)128>>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<size; bn++){
unsigned char byte=bytes[bn];
str[cn++]=_4bitsHex(byte%16, uppercase);
str[cn++]=_4bitsHex(byte/16, uppercase);
// left to right
if(inverse){
for(int32 bn=size-1; 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<size; bn++){ // byte number
unsigned char byte=bytes[bn];
str[cn++]=_4bitsHex(byte/16, uppercase);
str[cn++]=_4bitsHex(byte%16, uppercase);
}
}
str[cn]=0;
return str;
@ -139,9 +159,9 @@ char* toString_hex(void* _bytes, uint32 size, bool withPrefix, bool uppercase){
int##BITS n=*(int##BITS*)_n;\
return toString_int(n);\
case kp_b:\
return toString_bin(_n, BITS/8, kp_fmt_withPrefix(f));\
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\
case kp_h:\
return toString_hex(_n, BITS/8, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
default:\
kprintf("\n%u\n", kp_fmt_dataFormat(f));\
throw(ERR_FORMAT);\
@ -159,9 +179,9 @@ __toString_int_def(64)
uint##BITS n=*(uint##BITS*)_n;\
return toString_uint(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\
case kp_b:\
return toString_bin(_n, BITS/8, kp_fmt_withPrefix(f));\
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\
case kp_h:\
return toString_hex(_n, BITS/8, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
default:\
kprintf("\n%u\n", kp_fmt_dataFormat(f));\
throw(ERR_FORMAT);\
@ -179,9 +199,9 @@ __toString_uint_def(64)
float##BITS n=*(float##BITS*)_n;\
return toString_float(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\
case kp_b:\
return toString_bin(_n, BITS/8, kp_fmt_withPrefix(f));\
return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\
case kp_h:\
return toString_hex(_n, BITS/8, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\
default:\
kprintf("\n%u\n", kp_fmt_dataFormat(f));\
throw(ERR_FORMAT);\

View File

@ -27,9 +27,10 @@ char* toString_float(float64 n, bool withPostfix, bool uppercase); // very bad i
char* __toString_float32(void* n, uint32 fmt);
char* __toString_float64(void* n, uint32 fmt);
// universal functions
char* toString_bin(void* bytes, uint32 size, bool withPrefix);
char* toString_hex(void* bytes, uint32 size, bool withPrefix, bool uppercase);
///@param inverse set to true for little endian numbers (their bytes are in reverse order)
char* toString_bin(void* bytes, uint32 size, bool inverse, bool withPrefix);
///@param inverse set to true for little endian numbers (their bytes are in reverse order)
char* toString_hex(void* bytes, uint32 size, bool inverse, bool withPrefix, bool uppercase);
#if __cplusplus
}

View File

@ -93,7 +93,7 @@ void kprintf(const char* format, ...){
case 'p':
case 'x': ;
uint64 px=va_arg(vl, uint64);
argstr=toString_hex(&px,sizeof(px),1,0);
argstr=toString_hex(&px,getEndian()==LittleEndian,sizeof(px),1,0);
break;
case 's': ;
char* cptr=va_arg(vl,char*);