Conversor binário
Publicado por Geraldo José Ferreira Chagas Júnior 08/09/2008
[ Hits: 10.062 ]
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);
}
}
Nenhum comentário foi encontrado.
LazyDocker – Interface de Usuário em Tempo Real para o Docker
Instalando COSMIC no Linux Mint
Turbinando o Linux Mint: o poder das Nemo Actions
Inteligência Artificial no desenvolvimento de software: quando começar a usar?
[Resolvido] Algo deu errado ao abrir seu perfil
Usando o VNSTAT para medir o seu consumo de internet
Habilitando clipboard manager no ambiente COSMIC
Problema com som no laptop (5)
Quando vocês pararam de testar distros? (11)
Não estou conseguindo fazer funcionar meu Postfix na versão 2.4 no Deb... (2)









