Conversor binário
Publicado por Geraldo José Ferreira Chagas Júnior 08/09/2008
[ Hits: 10.075 ]
Homepage: http://prginfo.blogspot.com
Converte um número em binário para decimal, hexadecimal e quando possível, mostra o caractere referente ao valor ASCII.
#include <stdio.h>
#include <string.h>
#define iifi(c) (((c)=='0') ? (0) : (1))
void intTohex (unsigned long int b, char* hex)
{
char vet[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
unsigned long int r;
r = b % 0x10; hex[7] = vet[r]; b = b >> 0x4;
r = b % 0x10; hex[6] = vet[r]; b = b >> 0x4;
r = b % 0x10; hex[5] = vet[r]; b = b >> 0x4;
r = b % 0x10; hex[4] = vet[r]; b = b >> 0x4;
r = b % 0x10; hex[3] = vet[r]; b = b >> 0x4;
r = b % 0x10; hex[2] = vet[r]; b = b >> 0x4;
r = b % 0x10; hex[1] = vet[r]; b = b >> 0x4;
hex[0] = vet[b];
hex[8] = '{FONTE}';
}
unsigned long int binToInt (char *str)
{
return iifi(str[0])*128 + iifi(str[1])*64 + iifi(str[2])*32 + iifi(str[3])*16 + iifi(str[4])*8 + iifi(str[5])*4 + iifi(str[6])*2 + iifi(str[7]);
}
void arruma (char *dest, char *ori)
{
int i;
int t;
t=strlen(ori)-1;
for (i=7; i>=0;i--)
{
if (t>=0) dest[i]=ori[t]; else dest[i]='0';
t--;
}
dest[8]='{FONTE}';
}
char inToChar (unsigned long int val)
{
if ((val > 32) && (val < 127)) return val; else return '.';
}
int main (int argc, char* argv[])
{
char temp[9];
unsigned long int valInt;
char c;
int i;
if (argc<2)
{
printf ("Parametro incorreto.\n");
printf ("Informe apenas os valores em binário\n");
return 1;
}
for (i=1; i<argc;i++)
{
arruma(temp, argv[i]);
valInt=binToInt(temp);
intTohex(valInt,temp);
c = inToChar (valInt);
printf ("Inteiro=%d Hexa=%s char=%c\n",valInt, temp, c);
}
}
EXPRESSÕES ARITMÉTICAS - PARTE 1
Nenhum comentário foi encontrado.
Cinnamon seria a aposta acertada frente às outras interfaces gráficas mais populares?
KDE Plasma - porque pode ser a melhor opção de interface gráfica
Gentoo: detectando impressoras de rede e como fixar uma impressora por IP
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Vou destruir sua infância:) (8)
Interface gráfica com problema (2)
Instalar Linux em notebook Sony Vaio VPCEG13EB (13)









