niceBit

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

base58.h (695B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 typedef unsigned char byte;
      5 
      6 /* See https://en.wikipedia.org/wiki/Positional_notation#Base_conversion */
      7 char *base58(byte *s, int s_size, char *out, int out_size) {
      8     static const char *base_chars = "123456789"
      9                                     "ABCDEFGHJKLMNPQRSTUVWXYZ"
     10                                     "abcdefghijkmnopqrstuvwxyz";
     11 
     12     byte s_cp[s_size];
     13     memcpy(s_cp, s, s_size);
     14 
     15     int c, i, n;
     16 
     17     out[n = out_size] = 0;
     18     while (n--) {
     19         for (c = i = 0; i < s_size; i++) {
     20             c = c * 256 + s_cp[i];
     21             s_cp[i] = c / 58;
     22             c %= 58;
     23         }
     24         out[n] = base_chars[c];
     25     }
     26 
     27     return out;
     28 }