Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert FR escaped strings to hex
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
unsigned int verbose = 0;
unsigned int octal = 0;
char *p,*q;
char strbuff[254];
size_t s;
unsigned int octbuff[3];
unsigned int *power;
power = octbuff;
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'v')
verbose = 1;
printf("0x");
while ((s = read(STDIN_FILENO, strbuff, sizeof(strbuff))) > 0) {
p = strbuff;
q = p + s;
while (q - p) {
if (p[0] == '\\') {
power = octbuff;
octal = 1;
} else if (octal) {
*power++ = ((int) p[0]) - 48;
if (power - octbuff == 3) {
if (verbose)
printf("%c%c%c%c\t>\t", '\\', octbuff[0] + 48, octbuff[1] + 48, octbuff[2] + 48);
printf("%02x", (octbuff[0] * 8 * 8) + (octbuff[1] * 8) + octbuff[2]);
if (verbose)
printf("\n");
power = octbuff;
octal = 0;
}
}
else {
if (verbose)
printf("%c\t>\t", p[0]);
printf("%02x", (int) p[0]);
if (verbose)
printf("\n");
}
p++;
}
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment