gpt4 book ai didi

linux - C++ 十六进制转换器代码中的逻辑错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:27:33 24 4
gpt4 key购买 nike

我一直在研究这个十六进制转换器,但程序中似乎存在逻辑错误。我已经使用 g++ 工具在 Ubuntu 上运行它,每次运行 t 程序时,它都会给我一大堆垃圾值。我无法找出垃圾值的来源,也无法找到逻辑错误的来源。我是编程新手,所以请帮我找出我的错误。

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int bin[20],finhex[10],num,bc=0,i,j,k,l=0,r=10,n=1,binset=0,m=0;
int hex[16]= {0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,1011,1100,1101,1110,1111};
char hexalph='A';
cout<<"\nEnter your Number: ";
cin>>num;
while(num>0)
{
bin[bc]=num%2;
num=num/2;
bc++;
}
if(bc%4!=0)
bc++;
for(j=0;j<bc/4;j++)
for(i=0;i<4;i++)
{
binset=binset+(bin[m]*pow(10,i));
m++;
}
for(k=0;k<16;k++)
{
if(hex[k]==binset)
{
if(k<=9)
finhex[l]=k;
else
while(n>0)
{
if(k==r)
{
finhex[l]=hexalph;
break;
}
else
{
hexalph++;
r++;
}
}
l++;
r=10;
binset=0;
hexalph='A';
break;
}
}
while(l>=0)
{
cout<<"\n"<<finhex[l];
l--;
}
return 0;
}

最佳答案

int hex[16]= {0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,1011,1100,1101,1110,1111};

请允许我为您将这些值转换为十进制:

int hex[16] = {0, 1, 8, 9, 64, 65, 72, 73, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111};

如果您希望它们被视为二进制文字,那么您需要照原样指定它们或将它们放入编译器可以理解的其他形式:

int hex[16] = {0b0000, 0b0001, 0b0010, 0b0011, 0b0100, 0b0101, 0b0110, 0b0111, 0b1000, 0b1001, 0b1010, 0b1011, 0b1100, 0b1101, 0b1110, 0b1111};

int hex[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};

关于linux - C++ 十六进制转换器代码中的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41089324/

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