设计一个程序,输入一个十进制整数,输出相应的十六进制数。设计一个函数实现数制转换。

用数组算,菜鸟球叫你
2024-11-12 11:47:04
推荐回答(3个)
回答1:

//C语言实现:

#include
void main()
{char b[]="0123456789ABCDEF";
long int n,x;
int i,a[40];
printf("\n Please input n=");
scanf("%ld",&n);
x=n;i=0;
while(x>0)
{a[i]=x%16;
x=x/16;
i++;
}
while(i>0)
{printf("%c",b[a[i-1]]);
i--;
}
printf("\n");
}

对于大数转换可以用如下程序:

#include "Stdio.h"
#include "Conio.h"
char result[100];
int pos;
void change(int *num);
int main(void)
{
char str[100];
int num[100];
int bin[300]={0};
int quot[100],residue=0;
int i,j,k=0,suffix=0,index=0;
puts("Input a decimal number:");
gets(str);
for(i=0;i j=i-1;
while(j>=0)
{
num[j]%2?(bin[k++]=1):(bin[k++]=0);
while(index<=j)
{
residue+=num[index];
if(residue>=2)
quot[suffix++]=residue/2;
else if(index)
quot[suffix++]=0;
residue=(num[index]%2)*10;
index++;
}
for(i=0;i j=suffix-1;
residue=suffix=index=0;
}
for(i=0;i for(i=pos-1;i>=0;printf("%c",result[i]),i--);
getch();
return 0;
}
void change(int *num)
{
if(num[3]==0&&num[2]==0&&num[1]==0&&num[0]==0)
sprintf(result+pos,"%c",'0');
if(num[3]==0&&num[2]==0&&num[1]==0&&num[0]==1)
sprintf(result+pos,"%c",'1');
if(num[3]==0&&num[2]==0&&num[1]==1&&num[0]==0)
sprintf(result+pos,"%c",'2');
if(num[3]==0&&num[2]==0&&num[1]==1&&num[0]==1)
sprintf(result+pos,"%c",'3');
if(num[3]==0&&num[2]==1&&num[1]==0&&num[0]==0)
sprintf(result+pos,"%c",'4');
if(num[3]==0&&num[2]==1&&num[1]==0&&num[0]==1)
sprintf(result+pos,"%c",'5');
if(num[3]==0&&num[2]==1&&num[1]==1&&num[0]==0)
sprintf(result+pos,"%c",'6');
if(num[3]==0&&num[2]==1&&num[1]==1&&num[0]==1)
sprintf(result+pos,"%c",'7');
if(num[3]==1&&num[2]==0&&num[1]==0&&num[0]==0)
sprintf(result+pos,"%c",'8');
if(num[3]==1&&num[2]==0&&num[1]==0&&num[0]==1)
sprintf(result+pos,"%c",'9');
if(num[3]==1&&num[2]==0&&num[1]==1&&num[0]==0)
sprintf(result+pos,"%c",'A');
if(num[3]==1&&num[2]==0&&num[1]==1&&num[0]==1)
sprintf(result+pos,"%c",'B');
if(num[3]==1&&num[2]==1&&num[1]==0&&num[0]==0)
sprintf(result+pos,"%c",'C');
if(num[3]==1&&num[2]==1&&num[1]==0&&num[0]==1)
sprintf(result+pos,"%c",'D');
if(num[3]==1&&num[2]==1&&num[1]==1&&num[0]==0)
sprintf(result+pos,"%c",'E');
if(num[3]==1&&num[2]==1&&num[1]==1&&num[0]==1)
sprintf(result+pos,"%c",'F');
pos++;
}

回答2:

create function hex(@cardno int )
returns varchar(100)
as
begin
declare @temp_mod int
declare @i int
declare @result varchar(100)
declare @temp_x int
declare @result_values int

set @result=''
set @i=1
set @temp_x=0

while @cardno>0
begin
set @temp_mod=@cardno%16
set @cardno=@cardno/16
set @result=(case @temp_mod when 10 then 'A'
when 11 then 'B'
when 12 then 'C'
when 13 then 'D'
when 14 then 'E'
when 15 then 'F'
when 16 then 'G'
when 17 then 'H'
when 18 then 'I' else ltrim(str(@temp_mod)) end )+@result
end

--以上是把十进制转成十六进制
if len(@result)>=6
set @result=right(@result,6)

while len(@result)-@i>=0
begin
set @temp_x=(case len(@result)-@i when 1 then 16
when 2 then 16*16
when 3 then 16*16*16
when 4 then 16*16*16*16
when 5 then 16*16*16*16*16
when 6 then 16*16*16*16*16*16
when 7 then 16*16*16*16*16*16*16
when 8 then 16*16*16*16*16*16*16*16 else 1 end )

set @result_values=@result_values+(case substring(@result,@i,1) when 'A' then 10
when 'B' then 11
when 'C' then 12
when 'D' then 13
when 'E' then 14
when 'F' then 15
when 'G' then 16
when 'H' then 17
when 'I' then 18
else substring(@result,@i,1) end)*@temp_x

set @i=@i+1
end
--以上是把十六进制转成十进制

回答3:

public class Convert{
public static void main(String[] args){
System.out.println(toHex(12345));
}
public static String toHex(int x){
String result="";
while(x>0){
result=(char)(x%16>9?x%16+55:x%16+48)+result;
x/=16;
}
return result;
}
}