C/C++教程

串口编程 - ascii字符与16进制的相互转换

本文主要是介绍串口编程 - ascii字符与16进制的相互转换,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

串口编程 - ascii字符与16进制的相互转换


如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033

文章目录

  • 串口编程 - ascii字符与16进制的相互转换
    • 前言
    • 1. 16进制转换为16进制ascii字符
    • 2. ascii字符转换为16进制

前言

计算中所有数据的存储和运算都是使用二进制表示的,这种表示法便于计算机处理,但不便于人类理解。因此便产生了信息编码的概念。

信息编码(Information Coding),即用不同的代码与各种信息中的基本单位组成部分建立一一对应的关系。如计算机中常用的信息编码ASCII码。

串口通信中数据传输也都是使用二进制。为了便于展示或存储,会存在ascii字符与16进制之间的转换,如以ascii方式发送数据,对方以16进制方式进行展示或者以16进制方式发送,对方以ascii方式展示等。

1. 16进制转换为16进制ascii字符

用途:

  • 可用于串口数据接收时,将16进制数据转换为16进制ascii字符
  • 串口调试助手hex显示

示例:0x30 直接展示为 30 两个ascii码

#include <stdio.h>
#include <iostream>

int main()
{
	int recLen = 0;
	char hexChar[3];// two bit hex +  '\0'
	char * receiveStr = NULL;
	receiveStr = new char[1024];

	// mock read
    // recLen = p_sp->readAllData(str);
	receiveStr[0] = 0x00;
	receiveStr[1] = 0x11;
	receiveStr[2] = 0x22;
	receiveStr[3] = 0x33;
	receiveStr[4] = 0x44;
	recLen = 5;

	// convert to hex string
	for (int i = 0; i < recLen; i++)
	{
		snprintf(hexChar, sizeof(hexChar), "%02X", receiveStr[i] & 0xFF);// two bit hex + '\0'
		hexChar[sizeof(hexChar) - 1] = '\0';
		std::cout << hexChar;
	}

	if (receiveStr) 
	{
		delete[] receiveStr;
		receiveStr = NULL;
	}

    return 0;
}

2. ascii字符转换为16进制

用途:

  • 可用于串口发送时,获取ascii字符串对应的16进制数据
  • 串口调试助手hex发送

示例:ascii码0 转换为 0x30

char * HexStrFilter(char *charStr, char *fiterStr)
{
    if (charStr == NULL || fiterStr == NULL)
    {
        return NULL;
    }

    int len = strlen(charStr);

    char *pTemp = fiterStr;

    for(int i = 0; i < len; i++)
    {
        if( ((charStr[i] >= '0') && (charStr[i] <= '9')) ||
            ((charStr[i] >= 'A') && (charStr[i] <= 'F')) ||
            ((charStr[i] >= 'a') && (charStr[i] <= 'f')) )
        {
            *fiterStr++ = charStr[i];
        }
    }

    return pTemp;
}

char CombineHexChar(char charH,char charL) /// CombineHexChar(A,B) result=1011;
{
    char result;

    if(charH >= '0' && charH <= '9')
    {
        result = (charH - '0');
    }
    else if(charH >= 'a' && charH <= 'f')
    {
        result = (charH - 'a'+10);
    }
    else if(charH >= 'A' && charH <= 'F')
    {
        result = (charH - 'A' + 10);
    }
    else
    {
        result = 0;/// need to fiter non-hex character
    }

    result <<= 4;

    if(charL >= '0' && charL <= '9')
    {
        result += (charL - '0');
    }
    else if(charL >= 'a' && charL <= 'f')
    {
        result += (charL - 'a'+10);
    }
    else if(charL >= 'A' && charL <= 'F')
    {
        result += (charL - 'A' + 10);
    }
    else
    {
        result += 0;
    }

    return result;
}

int Char2Hex(char *charStr,char *hexStr)/// character to hex, return value is hexStr length
{
    if (charStr == NULL || hexStr == NULL)
    {
        return 0;
    }

    int hexStrCount = 0;

    char *fiterStr = NULL;
    fiterStr = new char[strlen(charStr) + 1];
    memset(fiterStr,0,strlen(charStr) + 1);

    HexStrFilter(charStr,fiterStr);///filter non-hex character

    int len = strlen(fiterStr);

    // warn: if charStr length not even, the last charactor will lost
    for(int i=0;i < len/2; i++)
    {
        *hexStr++ = CombineHexChar(fiterStr[i*2],fiterStr[i*2+1]);
        hexStrCount++;
    }

    if(fiterStr)
    {
        delete [] fiterStr;
        fiterStr = NULL;
    }

    return hexStrCount;
}

发送

char charStr[] = "0123456789";
 int len = strlen(charStr);
char *hexStr = NULL;
hexStr = new char[len + 1];
memset(hexStr,0,len + 1);

int hexStrLen = 0;
hexStrLen = Char2Hex(charStr,hexStr);

m_serialPort.writeData(hexStr,hexStrLen );

if(hexStr)
{
    delete [] hexStr;
    hexStr = NULL;


License

License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎

如需转载请标明出处:http://blog.csdn.net/itas109
QQ技术交流群:129518033


Reference:

  1. https://github.com/itas109/CSerialPort
  2. https://github.com/itas109/CSerialPort/issues/38
这篇关于串口编程 - ascii字符与16进制的相互转换的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!