Minor update:
- Added IntToBinStr() method to convert int to binary output
This commit is contained in:
34
UtilCore.cpp
34
UtilCore.cpp
@@ -119,6 +119,40 @@ char * BytesToBinStr( const char * Bytes, const int Len, const char * Separator,
|
|||||||
}
|
}
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
char * IntToBinStr( const int Bytes, const int Len, const char * Separator, char * OutBuf )
|
||||||
|
{
|
||||||
|
char * TempBuf;
|
||||||
|
char * BufPos;
|
||||||
|
bool First = true;
|
||||||
|
char SepLen = (Separator)? strlen(Separator) : 0;
|
||||||
|
u_int8_t Byte;
|
||||||
|
|
||||||
|
// Select/create output buffer
|
||||||
|
if (!OutBuf) {
|
||||||
|
TempBuf = ReturnStr;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// if (!*OutBuf)
|
||||||
|
// *OutBuf = (char*)malloc( Len*(8+SepLen)+1 );
|
||||||
|
// TempBuf = *OutBuf;
|
||||||
|
TempBuf = OutBuf;
|
||||||
|
}
|
||||||
|
BufPos = TempBuf;
|
||||||
|
|
||||||
|
// Print each byte as 8-bit binary
|
||||||
|
for (int i=Len-1; i>=0; i--) {
|
||||||
|
Byte = (Bytes >> (i*8)) & 0xFF;
|
||||||
|
sprintf( BufPos, "%s%c%c%c%c%c%c%c%c", ((First || !Separator)? "" : Separator),
|
||||||
|
(Byte & 0x80)?'1':'0', (Byte & 0x40)?'1':'0', (Byte & 0x20)?'1':'0', (Byte & 0x10)?'1':'0',
|
||||||
|
(Byte & 0x08)?'1':'0', (Byte & 0x04)?'1':'0', (Byte & 0x02)?'1':'0', (Byte & 0x01)?'1':'0' );
|
||||||
|
BufPos += (First)? 8 : 8+SepLen;
|
||||||
|
First = false;
|
||||||
|
}
|
||||||
|
*BufPos = 0;
|
||||||
|
return TempBuf;
|
||||||
|
}
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
char * HexStrToBytes( const char * Str, const int Len, const char * Separator, char * OutBuf )
|
char * HexStrToBytes( const char * Str, const int Len, const char * Separator, char * OutBuf )
|
||||||
{
|
{
|
||||||
char * TempBuf;
|
char * TempBuf;
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ char * BytesToSafeStr( const char * Bytes, const int Len, const bool NoCrLf = fa
|
|||||||
char * BytesToHexStr( const char * Bytes, const int Len, const char * Separator = " ", char * OutBuf = NULL );
|
char * BytesToHexStr( const char * Bytes, const int Len, const char * Separator = " ", char * OutBuf = NULL );
|
||||||
char * BytesToBinStr( const char * Bytes, const int Len, const char * Separator = " ", char * OutBuf = NULL );
|
char * BytesToBinStr( const char * Bytes, const int Len, const char * Separator = " ", char * OutBuf = NULL );
|
||||||
|
|
||||||
|
char * IntToBinStr( const int Bytes, const int Len, const char * Separator = " ", char * OutBuf = NULL );
|
||||||
|
|
||||||
// Convert string to raw bytes
|
// Convert string to raw bytes
|
||||||
inline u_int8_t HexToInt( char Digit ) {
|
inline u_int8_t HexToInt( char Digit ) {
|
||||||
if ((Digit >= '0') && (Digit <= '9')) return (Digit - '0');
|
if ((Digit >= '0') && (Digit <= '9')) return (Digit - '0');
|
||||||
|
|||||||
Reference in New Issue
Block a user