#include "stdafx.h"
void read_file_to_buff( CString file_src_dir,char *out_file_buff )
{
CFile fFileLoad;
int filelen = 0;
if( !fFileLoad.Open(file_src_dir, CFile::modeRead) );
return;
}
filelen = (UINT)fFileLoad.GetLength();
if (filelen > 100){
filelen = 100;
}
char *readbuf = new char[filelen + 1];
memset(readbuf, 0, filelen + 1);
fFileLoad.Read(readbuf, filelen);
fFileLoad.Close();
memcpy(out_file_buff, readbuf, filelen + 1);
delete readbuf;
}
int hexArrayToByteArray(char *str, u8 *byteArray)
{
int strLen = strlen(str);
int i, j = 0;
for (i = 0; i<strLen; i+=2,j++) {
byteArray[j] = 0;
char c = (char)str[i];
if ('0'<=c && c<='9') {
byteArray[j] |= ((c-'0')<<4);
} else if ('A'<=c && c<='F') {
byteArray[j] |= ((c-'A'+10)<<4);
} else if ('a'<=c && c<='f') {
byteArray[j] |= ((c-'a'+10)<<4);
} else {
}
c = (char)str[i+1];
if ('0'<=c && c<='9') {
byteArray[j] |= (c-'0');
} else if ('A'<=c && c<='F') {
byteArray[j] |= (c-'A'+10);
} else if ('a'<=c && c<='f') {
byteArray[j] |= (c-'a'+10);
} else {
}
}
return strLen/2;
}
void test_sprintf_s(void)
{
int i,j,k;
char stkey[11];
char tkey[5];
int count;
count = 11;
for( i=0x00;i<5;i++){
TRACE("test_sprintf_s count = %d.\n",count );
sprintf_s( (char *)&stkey[i*2], count, "%02X", (u8)tkey[0] );
TRACE("test_sprintf_s stkey = 0x%s.\n",stkey );
count = count - 2;
}
TRACE("test_sprintf_s stkey = 0x%s.\n",stkey );
}
void hexbuff_to_asciibuff( u8 *hexbuff, int hexbuff_len, char *asciibuff )
{
int remain_count,i;
remain_count = hexbuff_len*2+1;
for( i=0; i<hexbuff_len; i++ ){
sprintf_s( &asciibuff[i*2], remain_count, "%02X", hexbuff[i] );
remain_count = remain_count - 2;
}
asciibuff[i*2] = 0;
}
BYTE bData[512];
int Byte2HexChar(BYTE *bData, int iLen, unsigned char *data)
{
int ii = 0, jj = 0;
for (ii = 0; ii < iLen; ii++, jj += 2)
{
//sprintf_s(&data[jj], "%02x", bData[ii]);
//sprintf_s(bData, "%08x -", i + addr);
//sprintf_s((char *)&data[jj], "%02x", (unsigned char)bData[ii]);
}
data[jj] = 0;
return 0;
}
//------------------------------------------------------------------------------
// Funtion: ASCII 码转10 进制码
// Input : inputASCII - 输入 ASCII 码字符串
// Output : outputHex - 输出 10 进制数据
// Return : 10进制码的长度
// Info : 输入最大长度 1000 个字符
//------------------------------------------------------------------------------
UCHAR ASCIItoDex(char *inputASCII)
{
unsigned int bCount;
unsigned int m_DataLenth;
UCHAR Temp;
UCHAR Temp1;
if (0 == inputASCII)
{
return 0;
}
m_DataLenth = 0;
bCount = 0;
Temp1 = 0;
while (bCount <= 3)
{
Temp = inputASCII[bCount];
if (Temp == '\0')
{
return Temp1;
}
else if ((Temp >= '0') && (Temp <= '9'))
{
Temp -= 0x30;
}
else
{
return Temp1;
}
Temp1 = Temp1 * 10 + Temp;
bCount += 1;
}
return Temp1;
}
//文件存盘
void output_to_file(u8 *data, u32 len, CString file_path)
{
CFile pCFile;
CString filePath = file_path;
u32 file_size = len;
u32 file_off = 0;
u8 * out_file_content = new u8[file_size + 1];
::memset(out_file_content, '\0', file_size + 1);
::memcpy(out_file_content, &data[file_off], file_size);
if (!pCFile.Open(filePath, CFile::modeCreate | CFile::modeWrite)){
AfxMessageBox(_T("Open failed when write."));
return;
}
pCFile.Write(out_file_content, file_size);
pCFile.Close();
delete out_file_content;
}
void write_file_to_udisk(CString file_src_dir, CString file_dsc_dir)
{
CFile pCFile;
CString filePath = file_dsc_dir;
CFile fFileLoad;
UINT filelen = 0;
if( !fFileLoad.Open(file_src_dir, CFile::modeRead) ){
return;
}
filelen = (UINT)fFileLoad.GetLength();
char *readbuf = new char[filelen + 1];
memset(readbuf, 0, filelen + 1);
fFileLoad.Read(readbuf, filelen);
fFileLoad.Close();
u32 file_size = filelen;
u32 file_off = 0x00;
TRACE_Cstring("filePath = ", filePath);
if (!pCFile.Open(filePath, CFile::modeCreate | CFile::modeWrite)){
AfxMessageBox(_T("Open failed when write."));
return;
}
pCFile.Write(readbuf, file_size);
pCFile.Close();
delete readbuf;
}
void CString2char(CString instring, char *out_buff)
{
#if 0
int len = WideCharToMultiByte(CP_ACP, 0, instring, -1, NULL, 0, NULL, NULL);
char *ptxtTemp = new char[len + 1];
WideCharToMultiByte(CP_ACP, 0, instring, -1, ptxtTemp, len, NULL, NULL);
//TRACE( "%s%s\n","zhuanhuan ", ptxtTemp );
memcpy(out_buff, ptxtTemp, len);
delete[] ptxtTemp;
#endif
}
//UTF-8到CString的转换
int Utf8ToCString( const char *utf8, CString& c_str )
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
c_str = wstr;
if( wstr ){
delete[] wstr;
}
return len;
}