Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created December 17, 2019 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/1ec2b0988e1189ac77ea6104208b48f3 to your computer and use it in GitHub Desktop.
Save parzibyte/1ec2b0988e1189ac77ea6104208b48f3 to your computer and use it in GitHub Desktop.
/**
* Traductor de texto a binario en ANSI C
*
* @author parzibyte
* @see https://parzibyte.me/blog
* */
#include <stdio.h>
#include <string.h>
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char *itoa(int value, char *result, int base) {
// check that the base if valid
if (base < 2 || base > 36) {
*result = '\0';
return result;
}
char *ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 +
(tmp_value - value * base)];
} while (value);
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while (ptr1 < ptr) {
tmp_char = *ptr;
*ptr-- = *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
int main(void) {
// El texto que vamos a traducir
char *texto = "Mi nombre es Luis Cabrera Benito";
printf("En texto es: %s\n", texto);
char *relleno = "00000000";
// El número que irá tomando cada letra
int decimal = 0;
char cadena[9];
int i = 0;
printf("En binario es: \n");
while (texto[i] != '\0') {
decimal = (long) texto[i];// Obtener valor ASCII del carácter
// Convertir y almacenar el binario en "cadena"
itoa(decimal, cadena, 2);
// Diferencia para saber si no mide 8
int diferencia = 8 - (int) strlen(cadena);
// Imprimir. El %*.*s es para rellenar la cadena con ceros a la izquierda
// https://stackoverflow.com/questions/276827/string-padding-in-c/9741091#9741091
printf("%*.*s%s ", diferencia, diferencia, relleno, cadena);
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment