16,472
社区成员
发帖
与我相关
我的任务
分享
void parse(int i, char* buffer)
{
int a = i%10;
i = i/10;
int b = i%10;
i = i/10;
int c= i%10;
int d = i/10;
buffer[0] = d*16+c;
buffer[1] = b*16+a;
}
struct BCDByte
{
BCDByte(){}
BCDByte(unsigned int val){operator=(val);}
BCDByte & operator=(unsigned int val)
{
if(val > 99)
throw exception("BCDByte::operator=, val too large! val:" + val);
highbits = val / 10;
lowbits = val % 10;
return *this;
}
operator unsigned int() const {return highbits * 10 + lowbits;}
operator unsigned char() const {return highbits * 10 + lowbits;}
bool Check(){return highbits < 10 && lowbits < 10;}
private:
u_int8_t lowbits:4;
u_int8_t highbits:4;
};
int main()
{
BCDByte bytes[2];
int nTmp = 123;
bytes[0] = nTmp/100;
bytes[1] = nTmp%100;
//自己去看内存或者打印吧
}
char buf[8] = {0};
int nTmp = 123;
sprintf(buf,"0x%02x0x%02x",(int)nTmp/100,nTmp%100);
char buf[8] = {0};
int nTmp = 123;
sprintf(buf,"%02x%02x",(int)nTmp/100,nTmp%100);
sprintf(buf,"%02d%02d",(int)nTmp/100,nTmp%100);