Today I am coming for you with a complete app in C, it is simple but yet complete and do a great job.
For some reason I wanted to convert from ASCII to hex, I know that many Linux app already exist for that (oc, hexdump), but may be I found them hard to use correctly :)
The following is a simple but complete app you can use for that purpose, have fun :) :
/*
* @auther mhewedy
* @email mohammed_a_hewedy@hotmail.com
* @tested under gcc (GCC) 3.4.2 (mingw-special) (quite old :) )
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* usage(char*);
int main(int argc, char* argv[])
{
if (argc <= 1)
{
char* usage_string = usage(argv[0]);
printf("%s\n", usage_string);
free(usage_string);
exit(1);
}
int s_option_present =0;
char* string = NULL;
if (strcmp("-s", argv[1]) != 0)
string = argv[1];
else
{
string = argv[2];
s_option_present = 1;
}
while (*string != '\0')
{
printf("%X%s", *string++, (s_option_present? " " : ""));
}
printf("\n");
return 0;
}
char* usage(char* app_name)
{
char* usage = (char*) calloc(40, sizeof(char*));
strcpy(usage, "Usage: ");
strcat(usage, app_name);
strcat(usage, " [-s] {ascii string}");
return usage;
}
Example of usage:
atohex.exe -s ABC
atohex.exe -s 1
atohex.exe hello
No comments:
Post a Comment