工作总结2

1..strcpy提供了字符串的复制。即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符。
char* strcpy(char* dest, const char* src);

2.获取本地时间:
GetLocalTime(&InsertSysTime);//获取本地时间

3.十6进制转为10进制地址
sscanf(strAddr.Left(strAddr.Find('H')>0?strAddr.Find('H'):strAddr.Find('h')),
"%X", &m_nDeviceAddress)

4.年月日时分秒

function getDateStr(seconds){
var date = new Date(seconds*1000)
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var currentTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
return currentTime
}
//getDateStr(1524637692)>"2018-4-25 14:28:12"

//C++
SYSTEMTIME sys = {0};
sys = Time(lpszResp,nHead,n);

CTime cTime(sys);
time_t t = cTime.GetTime();

SYSTEMTIME CMyDevice::Time(LPCTSTR lpszResp,int nHead,int m)
{

/*double dbDaTa = 0;*/

//double myshort = 0;
SYSTEMTIME sys;
memset(&sys,0,sizeof(SYSTEMTIME));
int nData = 0;
CString strData = _T("");
char chData1[1] = {0};
char chData2[2] = {0};
char chData3[3] = {0};
char chData4[4] = {0};
char chData5[5] = {0};
char chData6[6] = {0};

char chDATA1 =(char)lpszResp[nHead + 19 + 36*m];
char chDATA2 =(char)lpszResp[nHead + 20 + 36*m];
char chDATA3 =(char)lpszResp[nHead + 21 + 36*m] ;
char chDATA4 =(char)lpszResp[nHead + 22 + 36*m];
char chDATA5 =(char)lpszResp[nHead + 23 + 36*m];
char chDATA6 =(char)lpszResp[nHead + 24 + 36*m];
/*strData = */
/*sscanf(chData,"%d",&nData);*/
sprintf(chData1,"%x",chDATA1);
sprintf(chData2,"%x",chDATA2);
sprintf(chData3,"%x",chDATA3);
sprintf(chData4,"%x",chDATA4);
sprintf(chData5,"%x",chDATA5);
sprintf(chData6,"%x",chDATA6);
int nTime1 = atoi(chData1);
int nTime2 = atoi(chData2);
int nTime3 = atoi(chData3);
int nTime4 = atoi(chData4);
int nTime5 = atoi(chData5);
int nTime6 = atoi(chData6);

sys.wYear = 2000 + nTime1;
sys.wMonth = nTime2 ;
sys.wDay = nTime3;
sys.wHour = nTime4;
sys.wMinute = nTime5;
sys.wSecond = nTime6;

return sys;

}

5.选择文件
原型为
CFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL);
调用:
CFileDialog dlg(true,NULL,NULL,NULL,"所有文件|*.*",NULL);

//char szFilter] = {"All Files(*.*)|*.*||"};
//CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,NULL);

if(IDOK == dlg.DoModal())
{
MessageBox(dlg.GetPathName(),_T(""));
//m_strXML = dlg.GetPathName();
}

6. CString strAddress = _T("");//实现把十进制数转为16进制数,并且逆序排列
UINT unAddress = atoi(intoData);

sprintf(strAddress.GetBuffer(0), "%0x", unAddress);
strAddress.ReleaseBuffer();

CString strAddress1 = _T("");
CString strAddress2 = _T("");
CString strAddress3 = _T("");
strAddress1 = strAddress.Right(2);
strAddress = strAddress.Left(strAddress.GetLength()-2);
strAddress2 = strAddress.Right(2);
strAddress3 = strAddress.Left(strAddress.GetLength()-2);

7.sscanf()的使用
strTime = strAddress.Left(2);//按十六进制转换
strAddress = strAddress.Mid(2);
int nData = 0;
sscanf(strTime,"%X",&nData);

strTime = strAddress.Left(1);//按字符串转换
strAddress = strAddress.Mid(1);
int nData = 0;
sscanf(strTime,"%s",&nData);

8.由16进制转为float类型

float freq;

char recv[4] = {0x41, 0xbc, 0x00, 0x00}; //接收到的数据,高字节到低字节排列

char * Modbus_HoldReg[4]; //定义保持寄存器指针数组


//第一步:指针初始化

Modbus_HoldReg0] = ((char*)(&freq))+3; //低地址指向高位

Modbus_HoldReg1] = ((char*)(&freq))+2;

Modbus_HoldReg2] = ((char*)(&freq))+1;

Modbus_HoldReg3] = ((char*)(&freq))+0; //高地址指向低位


//第二步:给地址指定的内存单元赋值(对应Modbus协议中的数据解析)

*Modbus_HoldReg0] = recv[0];

*Modbus_HoldReg1] = recv[1];

*Modbus_HoldReg2] = recv[2];

*Modbus_HoldReg3] = recv[3];
printf("%f ", freq);

9.字符转换

/**************************************** test ****************************************/
CString csMsg1 = _T("");
CString csMsg2 = _T("");
CString csTest = _T("测试字符串编码");
value.bstrVal = csTest.AllocSysString();
strText = value.bstrVal;
int Len = strText.GetLength();

char *TestBuff = new char[Len+1];

for (int i = 0 ; i < Len; i++)
{
TestBuffi] = (CHAR)strText.GetAt(i);
}
TestBuff[Len]='';
int TestLen = strlen(TestBuff);

//生成Unicode字符串
int nTestUnicodeLen = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)TestBuff, TestLen, NULL, 0);
wchar_t* pTestUnicodeBuff = new wchar_tnTestUnicodeLen+1];
memset(pTestUnicodeBuff, 0, (nTestUnicodeLen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)TestBuff, TestLen, (LPWSTR)pTestUnicodeBuff, nTestUnicodeLen);

if (!::IsTextUnicode(pTestUnicodeBuff, nTestUnicodeLen, NULL))
{
m_pDevice->ShowEventMessage("Convert] 非Unicode编码");
}

csMsg1 += "Convert] Hex1 = ";
csMsg2 += "Convert] Hex2 = ";
for (int nIndex = 0; nIndex<nTestUnicodeLen; nIndex++)
{
CString csTmp1 = _T("");
CString csTmp2 = _T("");
BYTE high, low;
high = (pTestUnicodeBuffnIndex] & 0xFF00) >> 8;
low = pTestUnicodeBuffnIndex] & 0x00FF;
csTmp1.Format("%02x %02x ", high, low);
csMsg1 += csTmp1;

// pTestUnicodeBuffnIndex] = (low<<8) | high;
// high = (pTestUnicodeBuffnIndex] & 0xFF00) >> 8;
// low = pTestUnicodeBuffnIndex] & 0x00FF;
// csTmp2.Format("%02x %02x ", high, low);
// csMsg2 += csTmp2;
}
m_pDevice->ShowEventMessage(csMsg1);
m_pDevice->ShowEventMessage(csMsg2);

CString csMessage = _T("");
csMessage.Format(_T("%s"), pTestUnicodeBuff);
m_pDevice->ShowEventMessage(csMessage);

//生成UTF-8字符串
int nTestUtf8Len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)pTestUnicodeBuff, -1, NULL, 0, NULL, NULL);
char* pTestUtf8Buff = (char*)malloc(nTestUtf8Len+1);
memset(pTestUtf8Buff, 0, nTestUtf8Len+1);
WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)pTestUnicodeBuff, -1, pTestUtf8Buff, nTestUtf8Len, NULL, NULL);
csMessage.Format(_T("%s"), pTestUtf8Buff);
m_pDevice->ShowEventMessage(csMessage);
/**************************************** test ****************************************/

中文操作系统默认ansi编码,生成的txt文件默认为ansi编码,所以,可以搜索出来。
unicode是国际通用编码,所以,可以搜索出来。
10.头文件

#ifndef GRAPHICS_H(头文件)
#define
#endif
#include "Filename.h"
//从用户工作目录开始搜索
#include <Filename.h>
//从标准库目录开始搜索

#include "myHeader.h"
void Function1();

class Vos
{
int n;
}

原文地址:https://www.cnblogs.com/zzw19940404/p/14058015.html