Conversor binário
Publicado por Geraldo José Ferreira Chagas Júnior 08/09/2008
[ Hits: 10.073 ]
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.
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
Gentoo binário em 2026: UEFI, LUKS, Btrfs e Systemd
Trabalhando Nativamente com Logs no Linux
Jogando Daikatana (Steam) com Patch 1.3 via Luxtorpeda no Linux
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)
Gentoo: Trocando wpa_supplicant pelo iwd no NetworkManager (Systemd)
Necessário autenticar ao imprimir - Ubuntu X Windowns (1)
O que houve com slackware ??? (11)
O Free Download Manager não abre no Fedora 43 KDE Plasma (2)









