Conversor binário
Publicado por Geraldo José Ferreira Chagas Júnior 08/09/2008
[ Hits: 10.051 ]
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);
}
}
Calcular taxa de juros com constante em C
Nenhum comentário foi encontrado.
Cirurgia para acelerar o openSUSE em HD externo via USB
Void Server como Domain Control
Modo Simples de Baixar e Usar o bash-completion
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
Script de montagem de chroot automatica
Atualizar Linux Mint 22.2 para 22.3 beta
Jogar games da Battle.net no Linux com Faugus Launcher
Como fazer a Instalação de aplicativos para acesso remoto ao Linux
Assisti Avatar 3: Fogo e Cinzas (4)
Conky, alerta de temperatura alta (11)









