一个16位的数以4位为一组分割,然后将各部分相加获取最终结果。
程序员文章站
2024-02-01 21:55:10
...
一个16位的数以4位为一组分割,然后将各部分相加获取最终结果。例如10进制的数据48633的16进制为BDF9,具体表现形式为1011 1101 1111 1001,运算结果为1011 + 1101 + 1111 + 1001.
#include <iostream>
unsigned int convert10to16(unsigned int data)
{
int qian = data / 8;
int bai = (data % 8 ) / 4;
int shi = (data % 8 ) % 4 / 2;
int ge = data %2;
return qian * 1000 + bai * 100 + shi * 10 + ge;
}
int main()
{
unsigned int tmp = 48633;
unsigned int data1 = tmp & 0xf;
unsigned int data2 = (tmp & 0xf0) >> 4;
unsigned int data3 = (tmp & 0xf00) >> 8;
unsigned int data4 = (tmp & 0xf000) >> 12;
std::cout << "data1:" << data1 << " data2:" << data2 << " data3:" << data3 << " data4:" << data4 << std::endl;
unsigned int binaryData1 = convert10to16(data1);
unsigned int binaryData2 = convert10to16(data2);
unsigned int binaryData3 = convert10to16(data3);
unsigned int binaryData4 = convert10to16(data4);
std::cout << "binaryData1:" << binaryData1 << " binaryData2:"<< binaryData2 << " binaryData3:" << binaryData3 << " binaryData4:" << binaryData4 << std::endl;
while(1);
return 0;
}
// 1011 1101 1111 1001