Valhalla Legends Forums Archive | C/C++ Programming | Ascii rep of binary string -> binary

AuthorMessageTime
iago
This is just a useful little program I wrote to convert an ascii representation of a binary string (say, "ff 0e 08 00 69 61 67 6f") to the binary equivolant.

If anybody has a better way, feel free to reply and tell me :)

[code]void main()
{
   int num;
   char test[] = "4e 64 40 32 45 12 15 00";

   for(char *p = strtok(test, " ");
      p;
      p = strtok(0, " "))
   {
      printf("%d: ", atoi(p));
      sscanf(p, "%x", &num);
      // (you can do stuff with num at this point)
      printf("%x\n", num);
   }
}[/code]

It's also a creative way to use a "for" in a non-standard way. Thanks to Adron for that idea, he told me how to do that a long time ago :)
August 10, 2003, 1:50 PM

Search