Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define PRINT_HEX(a) cout << hex << "Hex: 0x" << a << endl;
  4.  
  5. using namespace std;
  6. void printCharStrAsBytes(char* str){
  7. while(*str){
  8. cout << hex << (int)(*str++) << " ";
  9. }
  10. cout << endl;
  11. }
  12.  
  13. void printWCharStrAsBytes(wchar_t* str){
  14. while(*str){
  15. char* asChar = reinterpret_cast<char*>(str);
  16. for(size_t i = 0; i < sizeof(*str); i++){
  17. cout << hex << (int)asChar[i] << " ";
  18. }
  19. str++;
  20. }
  21. cout << endl;
  22. }
  23.  
  24. template<class T>
  25. union Union{
  26. T val;
  27. char bytes[sizeof(T)];
  28. };
  29.  
  30. template<class T>
  31. void printInHexWithOrder(T a){
  32. Union<T> u;
  33. u.val = a;
  34.  
  35. for(size_t i = 0; i < sizeof(T); i++){
  36. cout << hex << (int)u.bytes[i] << " ";
  37. }
  38. cout << endl;
  39. }
  40.  
  41. int main(){
  42. setlocale(0, "");
  43. int num = 0x12345678;
  44. printInHexWithOrder(num);
  45. char* ABCD = "abcd";
  46. cout << '\"' << ABCD << '\"' << " string stores in memory as: ";
  47. printCharStrAsBytes(ABCD);
  48.  
  49. char* RUS_ABCD = "абвг";
  50. cout << '\"' << RUS_ABCD << '\"' << " string stores in memory as: ";
  51. printCharStrAsBytes(RUS_ABCD);
  52.  
  53. wchar_t* W_ABCD = L"abcd";
  54. wcout << '\"' << W_ABCD << '\"' << " string stores in memory as: ";
  55. printWCharStrAsBytes(W_ABCD);
  56.  
  57. wchar_t* W_RUS_ABCD = L"абвг";
  58. wcout << '\"' << W_RUS_ABCD << '\"' << " string stores in memory as: ";
  59. printWCharStrAsBytes(W_RUS_ABCD);
  60. return 0;
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement