串口接收的ASCII数据如何转换成十进制
串口接收的ASCII数据怎么转换成十进制?
我在做一个接收串口的程序,接收到的数据是ASCII形式的,比如:接收到的数据是5149504654
而我需要把他转成312.6(这是十进制数)。
应该怎么转呢?
希望能给个具体的代码。谢谢
------最佳解决方案--------------------
//to_ansi_string ( 5149504654 )
//--to_ansi_string convert ansi format input data to string
//-- input = 51 49 50 46 54
//-- output = "QIPFT"
//
//-----最终计算结果 --- "QIPFT"
//
看起来您收到的数据,不是一个数字,确确实实是个字符串。
------其他解决方案--------------------
用atoi函数试试,其实和下位机定好协议,确定按二进制发送就行了,没必要下位机发ASCII码,这样下位机也麻烦,上位机还得转换.
------其他解决方案--------------------
怎么是字符串呢?
应该是数字的呀,ASCII的 51 49 50 46 54
对应的分别是:3 1 2 . 6的啊
------其他解决方案--------------------
这个肯定不行的呀,这个是to int,我要的是to double/float。
不过现在搞定了。
是自写的函数,先转换成十进制,再利用sscanf()函数转换成float就ok了。
我的的函数也贴出来。
CString strrecv="515349504654";//接收到的数据ASCII
CString strtemp="";//中间变量的保存
for(int i=0;i<(strrecv.GetLength());i++)
{
strtemp=strrecv.Mid(i,2);//每次取2个数字
Convert(strtemp);//转换成十进制
i+=1;
}
//**************************************************
CString strc="";//保存转换成的十进制结果
CString Convert(CString str)
{
if(str[0]=='4' && str[1]=='6')
{
strc+=".";
}else if(str[0]=='4' && str[1]=='8')
{
strc+="0";
}else if(str[0]=='4' && str[1]=='9')
{
strc+="1";
}else if(str[0]=='5' && str[1]=='0')
{
strc+="2";
}else if(str[0]=='5' && str[1]=='1')
{
strc+="3";
}else if(str[0]=='5' && str[1]=='2')
{
strc+="4";
}else if(str[0]=='5' && str[1]=='3')
{
strc+="5";
}else if(str[0]=='5' && str[1]=='4')
{
strc+="6";
}else if(str[0]=='5' && str[1]=='5')
{
strc+="7";
}else if(str[0]=='5' && str[1]=='6')
{
strc+="8";
}else if(str[0]=='5' && str[1]=='7')
{
strc+="9";
}
return strc;
}
我在做一个接收串口的程序,接收到的数据是ASCII形式的,比如:接收到的数据是5149504654
而我需要把他转成312.6(这是十进制数)。
应该怎么转呢?
希望能给个具体的代码。谢谢
------最佳解决方案--------------------
//to_ansi_string ( 5149504654 )
//--to_ansi_string convert ansi format input data to string
//-- input = 51 49 50 46 54
//-- output = "QIPFT"
//
//-----最终计算结果 --- "QIPFT"
//
看起来您收到的数据,不是一个数字,确确实实是个字符串。
------其他解决方案--------------------
用atoi函数试试,其实和下位机定好协议,确定按二进制发送就行了,没必要下位机发ASCII码,这样下位机也麻烦,上位机还得转换.
------其他解决方案--------------------
怎么是字符串呢?
应该是数字的呀,ASCII的 51 49 50 46 54
对应的分别是:3 1 2 . 6的啊
------其他解决方案--------------------
这个肯定不行的呀,这个是to int,我要的是to double/float。
不过现在搞定了。
是自写的函数,先转换成十进制,再利用sscanf()函数转换成float就ok了。
我的的函数也贴出来。
CString strrecv="515349504654";//接收到的数据ASCII
CString strtemp="";//中间变量的保存
for(int i=0;i<(strrecv.GetLength());i++)
{
strtemp=strrecv.Mid(i,2);//每次取2个数字
Convert(strtemp);//转换成十进制
i+=1;
}
//**************************************************
CString strc="";//保存转换成的十进制结果
CString Convert(CString str)
{
if(str[0]=='4' && str[1]=='6')
{
strc+=".";
}else if(str[0]=='4' && str[1]=='8')
{
strc+="0";
}else if(str[0]=='4' && str[1]=='9')
{
strc+="1";
}else if(str[0]=='5' && str[1]=='0')
{
strc+="2";
}else if(str[0]=='5' && str[1]=='1')
{
strc+="3";
}else if(str[0]=='5' && str[1]=='2')
{
strc+="4";
}else if(str[0]=='5' && str[1]=='3')
{
strc+="5";
}else if(str[0]=='5' && str[1]=='4')
{
strc+="6";
}else if(str[0]=='5' && str[1]=='5')
{
strc+="7";
}else if(str[0]=='5' && str[1]=='6')
{
strc+="8";
}else if(str[0]=='5' && str[1]=='7')
{
strc+="9";
}
return strc;
}