Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Going to binary to hex, and back (in C)

Name: Anonymous 2007-12-07 13:46

i want to write a little pseudo assembler in C. (please don't tell me how stupid of an idea this is) Is there a built-in function in C that allows me to go from ascii chars --> binary. And then go from binary --> hex. Something along the lines of how atoi() works? I couldn't find anything, it would save me some time though.

Name: Anonymous 2007-12-07 15:18


HAY GUYS I wrote a hex editor called shed that makes use of the following function I wrote. Its open sauce though, not pubic domain


 72 /* returns string representation of n in base 'base' */
   73 char *getstring(unsigned int n, char *s, int base, int width)
   74 {
   75   unsigned int i,j,c,d = calcwidth(n,base);
   76   char *p = s;
   77   if(width && d<width) {
   78     for(i=d;i<width;i++) {
   79       *p = '0';
   80       p++;
   81     }
   82   }
   83   for(i=d;i;i--) {
   84     j = pow(base,i-1);
   85     c = 0;
   86     while(n>=j) {
   87       n-=j;
   88       c++;
   89     }
   90     *p = c + ((c<10) ? 48 : 55);
   91     p++;
   92   }
   93   *p = 0;
   94   return s;


Removing the line numbers is left as an exercise for the reader

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List