Snippet Library

Browse | Submit A New Snippet | Create A Package

Binario -> Hexadecimal

Type:
Function
Category:
Math Functions
License:
GNU General Public License
Language:
C

Description:
Conversion de un Binario a un Hexadecimal, en agrupacion de 4 bits

Versions Of This Snippet:

Snippet ID Download Version Date Posted Author Delete
121.02005-04-22 04:21Andres Ovalle

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: 1.0

#include <stdio.h> #define dig 17 // Andres Ovalle int potencia(int base,int exp) { int acum=1; int i; if(exp == 0) return 1; else{ for(i=0;i<exp;i++) acum*=base; } return acum; } char *convert( char bin[]) { static char ret[dig]; int b=0,a=0,z=0,exp=0; int acum=0; for(b=0;b<dig;b++) { ret[b]=' '; ret[dig-1]='\0'; } for(b=dig-2,a=dig-2;b>=0;b--) { acum+=(bin[b]-0x30)*(potencia(2,exp)); if(z==3 || b==0) { ret[a]=(acum<10)?acum+48:acum+55; acum=0; exp=0; a--; z=0; } else{ exp++; z++; } } return ret; } void main(){ int pausa; char bin[dig]="1010000011110011"; printf("valor binario %s, a hex %s\n",bin,convert(bin)); scanf("%d",pausa); }

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..

Powered By FusionForge