Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 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. int num = 0x12345678;
  43. printInHexWithOrder(num);
  44.  
  45. printCharStrAsBytes("asfsaf");
  46.  
  47. cout << "WCHAR" << endl;
  48. printWCharStrAsBytes(L"Русская строка");
  49. cout << sizeof(wchar_t) << endl;
  50. return 0;
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement