Commit 80ed56a5 authored by Sam Schmalzried's avatar Sam Schmalzried

added zero padding and implemented binary to 64

parent d41cdb6e
No preview for this file type
......@@ -5,6 +5,7 @@
void hexToBinary(char*, char*);
char* hexQuads(char);
void binaryTo64(char*, char*);
void padZeroes(char*, int);
static const char quads[16][5] = {
"0000",
......@@ -34,13 +35,11 @@ int main(int argc, char* argv[]) {
}
size_t argSize = sizeof(argv[1]);
char* binaryString = malloc(4*(argSize+1));
char* binaryString = malloc(4*(argSize+2));
char* base64String = malloc(argSize);
hexToBinary(argv[1], binaryString);
printf("%s\n", binaryString);
binaryTo64(binaryString, base64String);
free(binaryString);
......@@ -53,7 +52,6 @@ void hexToBinary(char* hexString, char* binaryString) {
int i, len = strlen(hexString);
char c;
printf("len = %d\n", len);
for (i = 0; i < len; i++) {
c = *(hexString+i);
strcat(binaryString, hexQuads(c));
......@@ -70,5 +68,31 @@ char* hexQuads(char c) {
void binaryTo64(char* binaryString, char* base64String) {
printf("Now in binaryTo64\n");
int len = strlen(binaryString);
int zeroes = 6 - (len % 6);
int binaryNum, i;
char* binaryPiece = (char *) malloc(7 * sizeof(char));
if (zeroes != 6) {
padZeroes(binaryString, zeroes);
}
for (i = 0; i < len; i+=6) {
strncpy(binaryPiece, binaryString + i, 6*sizeof(char));
binaryNum = strtol(binaryPiece, NULL, 2);
strncat(base64String, cb64+binaryNum, 1);
printf("%s\n", base64String);
}
}
void padZeroes(char* binaryString, int numZeroes) {
int size = sizeof(binaryString) + numZeroes, i;
char* tempString = malloc(size);
for (i = 0; i < numZeroes; i++) {
strcat(tempString, "0");
}
strcat(tempString, binaryString);
strcpy(binaryString, tempString);
return;
}
\ No newline at end of file
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment