Skip to content

Instantly share code, notes, and snippets.

Created January 31, 2010 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/291263 to your computer and use it in GitHub Desktop.
Save anonymous/291263 to your computer and use it in GitHub Desktop.
#include <stdio.h>
//takes a string to read, a byte array to put stuff into, and the number of
//bytes requested (strlen(input)/2)
void compute(char *input, int *into, int num) {
int i;
char buf[3]; buf[2] = '\0';
for(i=0; i<num*2; i+=2) {
buf[0] = input[i];
buf[1] = input[i+1];
into[i==0 ? 0 : i/2] = strtol(buf,0,16);
}
}
int main(int argc, char **argv)
{
if(argc < 2) {
fprintf(stderr, "put the numbers as first argument.\n");
return 1;
}
char *input = argv[1];
//the number of bytes we want in the end
int num = 6;
//make the result array the correct size
int result[num];
//send the input to compute, which fills result with the numbers
compute(input, &result[0], num);
int i = 0;
for(i = 0; i < 6; i++) {
printf("%02x\n", result[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment