Convert hexadecimal to base64
I am currently working on a hex-> base64 converter. How can I deal with an odd number of hexadecimal digits? What I have been doing so far is that each hexadecimal digit is a 4-bit number, so 2 hexadecimal digits are 1 byte. If I come across an odd number of hexadecimal digits, will I just fill in the remainder of the unfinished byte with 0? Or should I return an error?
source to share
I accomplished this task in C. I am using a loop to remove leading zeros or for that matter.
#include <stdio.h>
#include <stdlib.h>
char* hex_to_base64(char *hex, int size)
{
int size64 = (size * 2) / 3.0;
size64 += 1;
char *base64 = calloc(size64, 1);
size64 -= 1;
for (int i = size-1; i>= 0; i-=3, size64-=2) {
base64[size64] |= hex[i];
if (i > 0) {
base64[size64] |= ((hex[i - 1] << 4) & 0x3F); //0x3F is 00111111
base64[size64 - 1] |= (hex[i - 1] >> 2);
}
if (i > 1) {
base64[size64 - 1] |= ((hex[i - 2] << 2));
}
}
return base64;
}
int main(int argc, char **argv)
{
int i = 0;
//49276D206B696C6C696E6720796F757220627261696E206C696B65206120706F69736F6E6F7573206D757368726F6F6D
char input[] = { 4, 9, 2, 7, 6, 13, 2, 0, 6, 11, 6, 9, 6, 12, 6, 12, 6, 9, 6, 14, 6, 7, 2, 0, 7, 9, 6, 15, 7, 5, 7, 2, 2, 0, 6, 2, 7,
2, 6, 1, 6, 9, 6, 14, 2, 0, 6, 12, 6, 9, 6, 11, 6, 5, 2, 0, 6, 1, 2, 0, 7, 0, 6, 15, 6, 9, 7, 3, 6, 15, 6, 14, 6, 15, 7, 5, 7, 3,
2, 0, 6, 13, 7, 5, 7, 3, 6, 8, 7, 2, 6, 15, 6, 15, 6, 13 };
char *output;
int outputsize = ((sizeof(input)* 2) / 3.0) + 1;
char *text = calloc(outputsize + 1, 1);
char *formatted;
output = hex_to_base64(input, sizeof(input));
for (i = outputsize-1; i >=0; i--) {
if (output[i] < 26) {
text[i] = output[i] + 65;
}
else if (output[i] < 52) {
text[i] = output[i] + 97 - 26;
}
else if (output[i] < 62) {
text[i] = output[i] + 48 - 52;
}
else if (output[i] == 62) {
text[i] = '+';
}
else if (output[i] == 63) {
text[i] = '/';
}
}
i = 0;
formatted = text;
while (text[i++] == 'A') {
formatted++;
}
printf("%s\n", formatted);
free(text);
return 0;
}
source to share
Discovered this issue while working on crypto fingers. The answer, according to Wikipedia:
- Add padding so the final string is divisible by 3 bytes.
- Set 4-bit values ββto 0 (depending on how you do it before or after converting the hex string, this is the actual value 0 or "0")
- In the resulting base64 string, the number of padded 4-bit values ββis marked with the same number of '=' at the end
The implementation of 0x41414141 does not appear to be a complete solution to the problem, as the conversion from hexstring to binary is hand-hardcoded. Also, I don't understand why the leading zeros should be removed.
source to share