fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void MulBytesBy10(unsigned char* buf, size_t cnt)
  5. {
  6. unsigned carry = 0;
  7. while (cnt--)
  8. {
  9. carry += 10 * *buf;
  10. *buf++ = carry & 0xFF;
  11. carry >>= 8;
  12. }
  13. }
  14.  
  15. void AddDigitToBytes(unsigned char* buf, size_t cnt, unsigned char digit)
  16. {
  17. unsigned carry = digit;
  18. while (cnt-- && carry)
  19. {
  20. carry += *buf;
  21. *buf++ = carry & 0xFF;
  22. carry >>= 8;
  23. }
  24. }
  25.  
  26. void DecimalIntegerStringToBytes(unsigned char* buf, size_t cnt, const char* str)
  27. {
  28. memset(buf, 0, cnt);
  29.  
  30. while (*str != '\0')
  31. {
  32. MulBytesBy10(buf, cnt);
  33. AddDigitToBytes(buf, cnt, *str++ - '0');
  34. }
  35. }
  36.  
  37. void PrintBytesHex(const unsigned char* buf, size_t cnt)
  38. {
  39. size_t i;
  40. for (i = 0; i < cnt; i++)
  41. printf("%02X", buf[cnt - 1 - i]);
  42. }
  43.  
  44. int main(void)
  45. {
  46. unsigned char buf[16];
  47.  
  48. DecimalIntegerStringToBytes(buf, sizeof buf, "664813035583918006462745898431981286737635929725");
  49.  
  50. PrintBytesHex(buf, sizeof buf); puts("");
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 5392KB
stdin
Standard input is empty
stdout
346C773479735F4834735F6233336E7D