起因
Modelsim中的结果是以十六进制有符号数的形式给出的,不方便观察,所以想把它转成十进制数,然而没有找到合适的工具,无奈自己用C语言写了一个。
运行结果
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* fun(char ch);
char str1[] = "1234";
int main()
{
int width = 16;
char hex[] = "ffffffff";
char *complement;
char *t_complement;
char *reverse;
char *original;
int B_num = 0;
int data = 0;
int scanf_len = 0;
int i;
while(1){
printf("\n请输入宽度:");
scanf("%d", &width);
printf("\n请输入十六进制数:");
scanf_len = scanf("%s", hex);
B_num = (width+3)/4;
if( strlen(hex)!= B_num)
{
printf("!!!输入错误!!!");
exit(0);
}
else
{
printf("十六进制数:%s\n", hex);
}
complement = (char*)malloc((B_num*4+5)*sizeof(char));
memset(complement, 0, sizeof(complement));
t_complement = (char*)malloc((width+5)*sizeof(char));
memset(t_complement, 0, sizeof(t_complement));
reverse = (char*)malloc((width+5)*sizeof(char));
memset(reverse, 0, sizeof(reverse));
original = (char*)malloc((width+5)*sizeof(char));
memset(original, 0, sizeof(original));
for(i=0; i<B_num; i++)
{
// ????????¡ã¨¦????????????????????¨¦????¡ã????????????
memcpy(complement+4*i, fun(hex[i]), 4);
}
complement[B_num*4] = '\0';
for(i=0; i<width; i++)
{
t_complement[i] = complement[i+(B_num*4-width)];
}
t_complement[width] = 0;
if(t_complement[0] == '1')
{
// ?¡¦????complement???¨¦??????????????
for(i=0+(B_num*4-width); i<B_num*4; i++)
{
if(i == (B_num*4-width))
reverse[i-(B_num*4-width)] = complement[i];
else if(complement[i] == '1')
reverse[i-(B_num*4-width)] = '0';
else
reverse[i-(B_num*4-width)] = '1';
}
reverse[width] = 0;
char carry = 1;
// ?¡¦????complement???¨¦??????????????
for(i=width-1; i>=0; i--)
{
if(i == 0)
original[i] = reverse[i];
else if(carry == 1){
if(reverse[i] == '1'){
original[i] = '0';
carry = 1;
}
else{
original[i] = '1';
carry = 0;
}
}
else{
if(reverse[i] == '1'){
original[i] = '1';
}
else{
original[i] = '0';
}
}
}
original[width] = 0;
}
else{
memcpy(reverse, t_complement, width);
memcpy(original, t_complement, width);
}
int base = 1;
for(i=width-1; i>=0; i--)
{
if(i == 0){
if(original[i] == '1')
data *= -1;
}
else{
data = data + (original[i]-'0')*base;
base *= 2;
}
}
// printf("%d %d %d", sizeof(int), sizeof(long int), sizeof(long long int));
printf("原码:%s\n", complement);
printf("原码:%s\n", t_complement);
printf("反码:%s\n", reverse);
printf("补码:%s\n", original);
printf("十进制数:%d\n", data);
printf("=================================================\n\n");
}
return 0;
}
char* fun(char ch)
{
switch(ch)
{
case '0':
memcpy(str1, "0000", 4);
break;
case '1':
memcpy(str1, "0001", 4);
break;
case '2':
memcpy(str1, "0010", 4);
break;
case '3':
memcpy(str1, "0011", 4);
break;
case '4':
memcpy(str1, "0100", 4);
break;
case '5':
memcpy(str1, "0101", 4);
break;
case '6':
memcpy(str1, "0110", 4);
break;
case '7':
memcpy(str1, "0111", 4);
break;
case '8':
memcpy(str1, "1000", 4);
break;
case '9':
memcpy(str1, "1001", 4);
break;
case 'a':
case 'A':
memcpy(str1, "1010", 4);
break;
case 'b':
case 'B':
memcpy(str1, "1011", 4);
break;
case 'c':
case 'C':
memcpy(str1, "1100", 4);
break;
case 'd':
case 'D':
memcpy(str1, "1101", 4);
break;
case 'e':
case 'E':
memcpy(str1, "1110", 4);
break;
case 'f':
case 'F':
memcpy(str1, "1111", 4);
break;
default:
printf("???????????¨ª?¨®??????\n");
exit(0);
break;
}
return str1;
}
attention
如果输入的宽度和字符串不匹配会自动结束程序。
都看到这儿了,点个赞呗
||
\/