Guest User

Untitled

a guest
Jan 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. char* DumpHex2(const void* data, size_t size) {
  4. const int symbolSize = 100;
  5. char* buffer = calloc(10*size, sizeof(char));
  6. char* symbol = calloc(symbolSize, sizeof(char));
  7.  
  8. char ascii[17];
  9. size_t i, j;
  10. ascii[16] = '\0';
  11. for (i = 0; i < size; ++i) {
  12. snprintf(symbol, symbolSize, "%02X ", ((unsigned char*)data)[i]);
  13. strcat(buffer, symbol);
  14. memset(symbol,0,strlen(symbol));
  15. if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
  16. ascii[i % 16] = ((unsigned char*)data)[i];
  17. } else {
  18. ascii[i % 16] = '.';
  19. }
  20. if ((i+1) % 8 == 0 || i+1 == size) {
  21. strcat(buffer, " ");
  22. if ((i+1) % 16 == 0) {
  23. snprintf(symbol, symbolSize, "| %s \n", ascii);
  24. strcat(buffer, symbol);
  25. memset(symbol,0,strlen(symbol));
  26. } else if (i+1 == size) {
  27. ascii[(i+1) % 16] = '\0';
  28. if ((i+1) % 16 <= 8) {
  29. strcat(buffer, " ");
  30. }
  31. for (j = (i+1) % 16; j < 16; ++j) {
  32. strcat(buffer, " ");
  33. }
  34. snprintf(symbol, symbolSize, "| %s \n", ascii);
  35. strcat(buffer, symbol);
  36. memset(symbol,0,strlen(symbol));
  37. }
  38. }
  39. }
  40.  
  41. free(symbol);
  42.  
  43. return buffer;
  44. }
Add Comment
Please, Sign In to add comment