• Welcome to Valhalla Legends Archive.
 

Ascii rep of binary string -> binary

Started by iago, August 10, 2003, 08:50 AM

Previous topic - Next topic

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 :)

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);
   }
}


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 :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*