Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define PRINT_HEX(a) cout << hex << "Hex: 0x" << a << endl;
- using namespace std;
- void printCharStrAsBytes(char* str){
- while(*str){
- cout << hex << (int)(*str++) << " ";
- }
- cout << endl;
- }
- void printWCharStrAsBytes(wchar_t* str){
- while(*str){
- char* asChar = reinterpret_cast<char*>(str);
- for(size_t i = 0; i < sizeof(*str); i++){
- cout << hex << (int)asChar[i] << " ";
- }
- str++;
- }
- cout << endl;
- }
- template<class T>
- union Union{
- T val;
- char bytes[sizeof(T)];
- };
- template<class T>
- void printInHexWithOrder(T a){
- Union<T> u;
- u.val = a;
- for(size_t i = 0; i < sizeof(T); i++){
- cout << hex << (int)u.bytes[i] << " ";
- }
- cout << endl;
- }
- int main(){
- setlocale(0, "");
- int num = 0x12345678;
- printInHexWithOrder(num);
- char* ABCD = "abcd";
- cout << '\"' << ABCD << '\"' << " string stores in memory as: ";
- printCharStrAsBytes(ABCD);
- char* RUS_ABCD = "абвг";
- cout << '\"' << RUS_ABCD << '\"' << " string stores in memory as: ";
- printCharStrAsBytes(RUS_ABCD);
- wchar_t* W_ABCD = L"abcd";
- wcout << '\"' << W_ABCD << '\"' << " string stores in memory as: ";
- printWCharStrAsBytes(W_ABCD);
- wchar_t* W_RUS_ABCD = L"абвг";
- wcout << '\"' << W_RUS_ABCD << '\"' << " string stores in memory as: ";
- printWCharStrAsBytes(W_RUS_ABCD);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement