1. 字符串16进制字符
AsicConvert(string,char);
//ASIC字符转十六进制函数
unsigned char AsicConvert (unsigned char i, unsigned char j)
{
switch (i)
{
case 0x30:return (j=0x00);break;
case 0x31:return (j=0x01);break;
case 0x32:return (j=0x02);break;
case 0x33:return (j=0x03);break;
case 0x34:return (j=0x04);break;
case 0x35:return (j=0x05);break;
case 0x36:return (j=0x06);break;
case 0x37:return (j=0x07);break;
case 0x38:return (j=0x08);break;
case 0x39:return (j=0x09);break;
case 0x41:
case 0x61:return (j=0x0A);break;
case 0x42:
case 0x62:return (j=0x0B);break;
case 0x43:
case 0x63:return (j=0x0C);break;
case 0x44:
case 0x64:return (j=0x0d);break;
case 0x45:
case 0x65:return (j=0x0e);break;
case 0x46:
case 0x66:return (j=0x0f);break;
case 0x20:return (' ');break;
default: return(j=0x10);break;
}
}
例:编辑框中TEST中输入字符B5,按十六进制0xB5,将值赋给输出变量outdata;
cstring sFind;
GetDlgItemText(IDC_EDIT_TEST, sFind);
unsigned char s1 = '1';
unsigned char s2 = '1';
unsigned char c1 = '1';
unsigned char c2 = '1';
s1 = sreadAdr.GetAt(0);
s2 = sreadAdr.GetAt(1);
c1 = AsicConvert(s1,c1);
c2 = AsicConvert(s2,c2);
unsigned char outdata;
outdata = (c1<<4) | c2;
2. 字符串转10进制函数
int ChangeNum (CString str, int length)
int ChangeNum (CString str, int length)
{
char revstr[16]={0};
int num[16]={0};
int count=1;
int result=0;
strcpy_s(revstr,str);
for (int i=length-1;i>=0;i--)
{
if ((revstr[i]>='0') && (revstr[i]<='9'))
num[i]=revstr[i]-48;//字符0的ASCII值为48
else if ((revstr[i]>='a') && (revstr[i]<='f'))
num[i]=revstr[i]-'a'+10;
else if ((revstr[i]>='A') && (revstr[i]<='F'))
num[i]=revstr[i]-'A'+10;
else
num[i]=0;
result=result+num[i]*count;
count=count*16;
}
return result;
}
3. 字符串转char型
CString to char *
strcpy(char,CString,sizeof(char));
https://www.cnblogs.com/bluestorm/p/3168720.html
自己写的函数:
char* EditDataCvtChar (CString strCnv, char * charRec)
//从编辑框取值转char型
char* EditDataCvtChar (CString strCnv, char * charRec)
{
//从RowNum编辑框取值
// CString RNum;
char * BSNum;
// unsigned char * BANum;
int CNumByte;
unsigned char k=0;
int i,j=0;
CNumByte=strCnv.GetLength();
BSNum = new char [CNumByte];
charRec = new char [CNumByte];
BSNum = (char*)(LPCSTR)strCnv;
for(i=0;i<(CNumByte/2);i++)
{
charRec[i] = (AsicConvert(BSNum[j],k)<<4) | AsicConvert(BSNum[j+1],k);
j += 2;
}
return charRec;
}
4. 字符串转浮点型方法:
CString sData;
float fData;
unsigned char * hData;
// send zone 0 data
OperDlgFlag = OPEREADSTATUS;
GetDlgItemText(IDC_EDIT_OPE_KP0,sData);//取KP zone0 编辑框中的数据
fData = (float)atof(sData);//将编辑框中的浮点字符串转成十六进制
hData = (unsigned char *)&fData;
OperBuf[0] = hData[0];
OperBuf[1] = hData[1];
OperBuf[2] = hData[2];
OperBuf[3] = hData[3];