从字符串中提取BCD码,转换为UINT数据并返回

C++ code:

#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

/*
	功能:将BCD码转换为UINT数据
	@p:			存储BCD码的字符串数组
	@len:		表示一个BCD码的字符长度
	@retValue:	保存一个BCD码的返回值
*/
char * BCDtoUINT(char *p, int len, UINT & retValue)
{
	retValue = 0;
	for(int i = 0; i < len; i++, p++)
	{
		retValue = retValue * 100 + (*p >> 4) * 10 + (*p & 0x0F);
	}
	return p;
}

void main()
{
	char a[10] = {0x00, 0x21, 0x01, 0x11, 0x26, 0x00, 0x09, 0x00, 0x00, 0x01};
	char * p = a;

	unsigned int seq[3];
	p = BCDtoUINT(p, 3, seq[0]);
	p = BCDtoUINT(p, 4, seq[1]);
	p = BCDtoUINT(p, 3, seq[2]);

	for(int i = 0; i < 3; i++)
	{
		cout<<seq[i]<<' ';
	}
	cout<<endl;
}

/*
	运行结果:
	2101 11260009 1
*/
原文地址:https://www.cnblogs.com/joeblackzqq/p/1890031.html