Skip to content

Instantly share code, notes, and snippets.

@forrestbthomas
Created May 8, 2015 03:21
Show Gist options
  • Save forrestbthomas/a14508ef72eb0843cbaa to your computer and use it in GitHub Desktop.
Save forrestbthomas/a14508ef72eb0843cbaa to your computer and use it in GitHub Desktop.
Hex to Base64 in C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int convert_int_to_base_64(int arg)
{
char result;
switch(arg)
{
case 0: result = 'A'; break;
case 1: result = 'B'; break;
case 2: result = 'C'; break;
case 3: result = 'D'; break;
case 4: result = 'E'; break;
case 5: result = 'F'; break;
case 6: result = 'G'; break;
case 7: result = 'H'; break;
case 8: result = 'I'; break;
case 9: result = 'J'; break;
case 10: result = 'K'; break;
case 11: result = 'L'; break;
case 12: result = 'M'; break;
case 13: result = 'N'; break;
case 14: result = 'O'; break;
case 15: result = 'P'; break;
case 16: result = 'Q'; break;
case 17: result = 'R'; break;
case 18: result = 'S'; break;
case 19: result = 'T'; break;
case 20: result = 'U'; break;
case 21: result = 'V'; break;
case 22: result = 'W'; break;
case 23: result = 'X'; break;
case 24: result = 'Y'; break;
case 25: result = 'Z'; break;
case 26: result = 'a'; break;
case 27: result = 'b'; break;
case 28: result = 'c'; break;
case 29: result = 'd'; break;
case 30: result = 'e'; break;
case 31: result = 'f'; break;
case 32: result = 'g'; break;
case 33: result = 'h'; break;
case 34: result = 'i'; break;
case 35: result = 'j'; break;
case 36: result = 'k'; break;
case 37: result = 'l'; break;
case 38: result = 'm'; break;
case 39: result = 'n'; break;
case 40: result = 'o'; break;
case 41: result = 'p'; break;
case 42: result = 'q'; break;
case 43: result = 'r'; break;
case 44: result = 's'; break;
case 45: result = 't'; break;
case 46: result = 'u'; break;
case 47: result = 'v'; break;
case 48: result = 'w'; break;
case 49: result = 'x'; break;
case 50: result = 'y'; break;
case 51: result = 'z'; break;
case 52: result = '0'; break;
case 53: result = '1'; break;
case 54: result = '2'; break;
case 55: result = '3'; break;
case 56: result = '4'; break;
case 57: result = '5'; break;
case 58: result = '6'; break;
case 59: result = '7'; break;
case 60: result = '8'; break;
case 61: result = '9'; break;
case 62: result = '+'; break;
case 63: result = '/'; break;
}
return result;
}
void convert_bin_to_int(char arg[])
{
unsigned long arg_len = strlen(arg) - 1;
int i;
int j;
double result = 0.00;
for (i = 0; i <= arg_len; i++)
{
if (arg[i] == 49)
{
result += pow(2, (arg_len - (i)));
}
}
printf("%c", convert_int_to_base_64(result));
}
void split_to_six_bits(char arg[])
{
char six_bit[7];
unsigned long arg_len = strlen(arg);
int i = 0;
int j = 0;
for (i = 0; i <= arg_len; i++) {
if (j == 6)
{
convert_bin_to_int(six_bit);
j = 0;
}
six_bit[j] = arg[i];
j++;
}
}
char* convert_to_binary(char arg)
{
char* result;
switch(arg)
{
case '0': result = "0000"; break;
case '1': result = "0001"; break;
case '2': result = "0010"; break;
case '3': result = "0011"; break;
case '4': result = "0100"; break;
case '5': result = "0101"; break;
case '6': result = "0110"; break;
case '7': result = "0111"; break;
case '8': result = "1000"; break;
case '9': result = "1001"; break;
case 'A': result = "1010"; break;
case 'B': result = "1011"; break;
case 'C': result = "1100"; break;
case 'D': result = "1101"; break;
case 'E': result = "1110"; break;
case 'F': result = "1111"; break;
case 'a': result = "1010"; break;
case 'b': result = "1011"; break;
case 'c': result = "1100"; break;
case 'd': result = "1101"; break;
case 'e': result = "1110"; break;
case 'f': result = "1111"; break;
}
return result;
}
void convert_hex(char arg[])
{
int i = 0;
char result[512];
for (i = 0; i < strlen(arg); i++)
{
if (i == 0)
{
strcpy(result, convert_to_binary(arg[i]));
}
else
{
strcat(result, convert_to_binary(arg[i]));
}
}
split_to_six_bits(result);
}
int main(int argc, char *argv[])
{
convert_hex(argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment