gpt4 book ai didi

c++ - 小端顺序转换为十六进制的函数时出错

转载 作者:行者123 更新时间:2023-12-02 10:12:00 25 4
gpt4 key购买 nike

我编写了一个进行转换的函数,但是有一个错误:输入1000时,E8 E3的字节序为小尾数,或者E3E8为十六进制,但是它应为03E8或小尾数格式的E8 03。
我真的不知道可能出什么问题了。
我知道代码的用途可能很大,但我真的很想知道错误出了什么问题,所有其他数字都可以正常工作,甚至是负数也可以

string decToHex(int num) {
string ret = "";
char c1='0', c2='0';
int res;
if(num>0){
while (num != 0) {
res = num % 16;
num = num / 16;
switch (res) {
case 0: c2 = '0'; break;
case 1: c2 = '1'; break;
case 2: c2 = '2'; break;
case 3: c2 = '3'; break;
case 4: c2 = '4'; break;
case 5: c2 = '5'; break;
case 6: c2 = '6'; break;
case 7: c2 = '7'; break;
case 8: c2 = '8'; break;
case 9: c2 = '9'; break;
case 10: c2 = 'A'; break;
case 11: c2 = 'B'; break;
case 12: c2 = 'C'; break;
case 13: c2 = 'D'; break;
case 14: c2 = 'E'; break;
case 15: c2 = 'F'; break;
}
if (num != 0) {
res = num % 16;
num = num / 16;
switch (res) {
case 0: c1 = '0'; break;
case 1: c1 = '1'; break;
case 2: c1 = '2'; break;
case 3: c1 = '3'; break;
case 4: c1 = '4'; break;
case 5: c1 = '5'; break;
case 6: c1 = '6'; break;
case 7: c1 = '7'; break;
case 8: c1 = '8'; break;
case 9: c1 = '9'; break;
case 10: c1 = 'A'; break;
case 11: c1 = 'B'; break;
case 12: c1 = 'C'; break;
case 13: c1 = 'D'; break;
case 14: c1 = 'E'; break;
case 15: c1 = 'F'; break;
}
}
ret += c1;
ret += c2; ret += " ";


}

return ret;
}
else if(num<0){
unsigned int n = num;
while (n != 0) {
res = n % 16;
n = n / 16;
switch (res) {
case 0: c2 = '0'; break;
case 1: c2 = '1'; break;
case 2: c2 = '2'; break;
case 3: c2 = '3'; break;
case 4: c2 = '4'; break;
case 5: c2 = '5'; break;
case 6: c2 = '6'; break;
case 7: c2 = '7'; break;
case 8: c2 = '8'; break;
case 9: c2 = '9'; break;
case 10: c2 = 'A'; break;
case 11: c2 = 'B'; break;
case 12: c2 = 'C'; break;
case 13: c2 = 'D'; break;
case 14: c2 = 'E'; break;
case 15: c2 = 'F'; break;
}
if (n != 0) {
res = n % 16;
n = n / 16;
switch (res) {
case 0: c1 = '0'; break;
case 1: c1 = '1'; break;
case 2: c1 = '2'; break;
case 3: c1 = '3'; break;
case 4: c1 = '4'; break;
case 5: c1 = '5'; break;
case 6: c1 = '6'; break;
case 7: c1 = '7'; break;
case 8: c1 = '8'; break;
case 9: c1 = '9'; break;
case 10: c1 = 'A'; break;
case 11: c1 = 'B'; break;
case 12: c1 = 'C'; break;
case 13: c1 = 'D'; break;
case 14: c1 = 'E'; break;
case 15: c1 = 'F'; break;
}
}
ret += c1;
ret += c2; ret += " ";
}
return ret;
}
else {
return "00 00 ";
}
}

最佳答案

我提出另一种解决方案,更短。

#include <iostream>
#include <string>


template <typename I> std::string n2hexstr(I w, int hex_len = sizeof(I)<<1) {
static const char* const digits = "0123456789ABCDEF";
std::string rc(hex_len,'0');

auto it = rc.begin();
for (int i = 0; i < hex_len; i++) {
*it++ = digits[(w >> (i*8 + 4)) & 0x0F];
*it++ = digits[(w >> (i*8)) & 0x0F];
}

return rc;
}


int main()
{
std::cout << n2hexstr( static_cast<uint16_t>(1000) ) << std::endl;
return 0;
}

关于c++ - 小端顺序转换为十六进制的函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63192454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com