void revert(char* psrc) {
size_t len = strlen(psrc);
if (len < 2) return;
char tmp = 0;
size_t i = 0, j = 0;
for (i = 0, j = len - 1; i < j; ++i, --j) {
tmp = psrc[i];
psrc[i] = psrc[j];
psrc[j] = tmp;
}
}
int tst5() {
char arr[128];
char arr_dst[128];
snprintf(arr, sizeof(arr), "%s", "10111111111000101010");
int src = 2;
int dst = 16;
revert(arr);
long t = 0;
long number = 0;
size_t i = 0;
int mode = 1;
for (i = 0; i < strlen(arr); ++i) {
if (arr[i] >= '0' && arr[i] <= '9') {
t = arr[i] - '0';
} else {
t = arr[i] - 'A' + 10;
}
dzlog_info("%lu", t);
number += mode * t;
mode *= src;
}
i = 0;
while (number) {
t = number % dst;
if (t >= 0 && t <= 9) {
arr_dst[i++] = t + '0';
} else {
arr_dst[i++] = t + 'A' - 10;
}
number /= dst;
}
revert(arr_dst);
return 0;
}
任意进制转换
最新推荐文章于 2021-05-12 16:12:28 发布