a64l.c 499 B

1234567891011121314151617181920212223242526272829
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. static const char digits[] =
  5. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  6. long a64l(const char *s)
  7. {
  8. int e;
  9. uint32_t x = 0;
  10. for (e=0; e<36 && *s; e+=6, s++) {
  11. const char *d = strchr(digits, *s);
  12. if (!d) break;
  13. x |= (uint32_t)(d-digits)<<e;
  14. }
  15. return (int32_t)x;
  16. }
  17. char *l64a(long x0)
  18. {
  19. static char s[7];
  20. char *p;
  21. uint32_t x = x0;
  22. for (p=s; x; p++, x>>=6)
  23. *p = digits[x&63];
  24. *p = 0;
  25. return s;
  26. }