-
c++ 十进制、十六进制和BCD的相互转换
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- using namespace std;
- int convert(unsigned char *dst, const unsigned char *src, int length)
- {
- int i;
- for (i = 0; i < length; i++)
- {
- dst[i] = src[i] ^ 0xFF;
- }
- return 0;
- }
- unsigned long HextoDec(const unsigned char *hex, int length)
- {
- int i;
- unsigned long rslt = 0;
- for (i = 0; i < length; i++)
- {
- rslt += (unsigned long)(hex[i]) << (8 * (length - 1 - i));
-
- }
- return rslt;
- }
-
- int DectoHex(int dec, unsigned char *hex, int length)
- {
- int i;
- for (i = length - 1; i >= 0; i--)
- {
- hex[i] = (dec % 256) & 0xFF;
- dec /= 256;
- }
- return 0;
- }
- unsigned long power(int base, int times)
- {
- int i;
- unsigned long rslt = 1;
- for (i = 0; i < times; i++)
- rslt *= base;
- return rslt;
- }
- unsigned long BCDtoDec(const unsigned char *bcd, int length)
- {
- int i, tmp;
- unsigned long dec = 0;
- for (i = 0; i < length; i++)
- {
- tmp = ((bcd[i] >> 4) & 0x0F) * 10 + (bcd[i] & 0x0F);
- dec += tmp * power(100, length - 1 - i);
- }
- return dec;
- }
- int DectoBCD(int Dec, unsigned char *Bcd, int length)
- {
- int i;
- int temp;
- for (i = length - 1; i >= 0; i--)
- {
- temp = Dec % 100;
- Bcd[i] = ((temp / 10) << 4) + ((temp % 10) & 0x0F);
- Dec /= 100;
- }
- return 0;
- }
- int main(int argc, char** argv)
- {
-
- unsigned char BCD[3] = { 0x00, 0x53, 0x20 };
- int dec_bcd = BCDtoDec(BCD, 3);
- cout << "dec_bcd : " << dec_bcd << endl;
-
-
- unsigned char tmp_bff[3] = "";
- DectoBCD(dec_bcd, tmp_bff, 3);
- for (int i = 0; i < 3; ++i)
- {
-
- printf("tmp_bff[%d] = 0x%02X
", i, tmp_bff[i]);
- }
-
- cout << endl << endl;
-
- unsigned char Hex[3] = { 0x00, 0x53, 0x20 };
- int dec_hex = HextoDec(Hex, 3);
- cout << "dec_hex: " << dec_hex << endl;
-
-
- unsigned char hex_bff[3] = "";
- DectoHex(dec_hex, hex_bff, 3);
- for (int i = 0; i < 3; ++i)
- {
- printf("hex_bff[%d] = 0x%02X
", i, hex_bff[i]);
- }
-
- system("pause");
- return 0;
- }
-
相关阅读:
caseStudy-20181216-Kafka(xxx)集群故障&解决办法
caseStudy-20190312 xxx kafka集群因文件描述符超阀值引起集群不可用
Kafka客户端二次封装扩展总体设计
2018年工作规划-Kafka方向OKR
针对Kafka的centos系统参数优化
脚本kafka-configs.sh用法解析
Kafka动态配置实现原理解析
动态配置实现原理解析参考资料
Topics类型配置
Brokers类型配置
-
原文地址:https://www.cnblogs.com/daochong/p/6532795.html
Copyright © 2020-2023
润新知