2024-11-11 15:28:31 +08:00
|
|
|
|
#include "kutilities.h"
|
2024-10-12 14:00:37 +08:00
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
|
|
Kutilities::Kutilities() {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 函数用于将内存块转换为十六进制字符串
|
|
|
|
|
std::string Kutilities::printHex(const void* data, size_t size)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
std::ostringstream oss2;
|
|
|
|
|
std::ostringstream ossrow;
|
|
|
|
|
|
|
|
|
|
// 每行输出的字节数
|
|
|
|
|
const size_t lineSize = 16;
|
|
|
|
|
const unsigned char* p = static_cast<const unsigned char*>(data);
|
|
|
|
|
|
|
|
|
|
int ic = 0;
|
|
|
|
|
int row = 0;
|
|
|
|
|
|
|
|
|
|
ossrow << std::setw(8) << std::setfill('0') << std::hex << row++ << "h : ";
|
|
|
|
|
oss << ossrow.str().c_str();
|
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < size; ++i )
|
|
|
|
|
{
|
|
|
|
|
ic++;
|
|
|
|
|
// 每个字节之间用空格分隔
|
|
|
|
|
oss << std::setw(2) << std::setfill('0') << std::hex << std::uppercase << static_cast<int>(p[i]);
|
|
|
|
|
|
|
|
|
|
char ch = (isprint(p[i]) != 0) ? p[i] : '.';
|
|
|
|
|
|
|
|
|
|
oss2 << ch;
|
|
|
|
|
|
|
|
|
|
//每lineSize个字节换行
|
|
|
|
|
if( (i + 1) % lineSize == 0 )
|
|
|
|
|
{
|
|
|
|
|
ossrow.clear();
|
|
|
|
|
ossrow.str("");
|
|
|
|
|
|
|
|
|
|
oss << " [" << oss2.str().c_str() << "]" << std::endl;
|
|
|
|
|
oss2.clear();
|
|
|
|
|
oss2.str("");
|
|
|
|
|
|
|
|
|
|
ossrow << std::setw(8) << std::setfill('0') << std::hex << row++ << "h : ";
|
|
|
|
|
oss << ossrow.str().c_str();
|
|
|
|
|
|
|
|
|
|
ic = 0;
|
|
|
|
|
}
|
|
|
|
|
else if( i == size - 1 )
|
|
|
|
|
{
|
|
|
|
|
if( (i + 1) % lineSize != 0 )
|
|
|
|
|
{
|
|
|
|
|
if( i % 2 != 0 )
|
|
|
|
|
{
|
|
|
|
|
for( size_t j = 0; j < (lineSize - ic); j++ )
|
|
|
|
|
{
|
|
|
|
|
oss << " --";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for( size_t j = 0; j < (lineSize - ic); j++ )
|
|
|
|
|
{
|
|
|
|
|
oss << " --";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
oss << " [" << oss2.str().c_str();
|
|
|
|
|
|
|
|
|
|
if( (i + 1) % lineSize != 0 )
|
|
|
|
|
{
|
|
|
|
|
for( size_t j = 0; j < (lineSize - ic); j++ )
|
|
|
|
|
{
|
|
|
|
|
oss << " ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oss << "]" << std::endl;
|
|
|
|
|
oss2.clear();
|
|
|
|
|
oss2.str("");
|
|
|
|
|
|
|
|
|
|
ic = 0;
|
|
|
|
|
}
|
|
|
|
|
#if 0
|
|
|
|
|
else if( (i + 1) % 8 == 0 )
|
|
|
|
|
{
|
|
|
|
|
oss << " ";
|
|
|
|
|
oss2 << " ";
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
oss << " ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return oss.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string Kutilities::get_current_timestamp()
|
|
|
|
|
{
|
|
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
|
//通过不同精度获取相差的毫秒数
|
|
|
|
|
uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count()
|
|
|
|
|
- std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() * 1000;
|
|
|
|
|
time_t tt = std::chrono::system_clock::to_time_t(now);
|
|
|
|
|
auto time_tm = localtime(&tt);
|
|
|
|
|
char strTime[25] = { 0 };
|
|
|
|
|
sprintf(strTime, "%d-%02d-%02d %02d:%02d:%02d.%03d", time_tm->tm_year + 1900,
|
|
|
|
|
time_tm->tm_mon + 1, time_tm->tm_mday, time_tm->tm_hour,
|
|
|
|
|
time_tm->tm_min, time_tm->tm_sec, (int)dis_millseconds);
|
|
|
|
|
return std::string(strTime);
|
|
|
|
|
}
|