Code to print the byte representation of program objects
2013-10-28 13:30
603 查看
This code uses casting to circumvent the type system. Firstly, cast pointers to byte_pointer, pointing to an object of type "unsigned char" which represents a single byte. Seondly, printf and cout are used to print the byte representation, and static_cast
is needed when using cout.
Running results on Win32 and x64 are represented on the two figures below.
Figure 1 Running results on Win32 Figure 2 Running results on x64
From the two figures, it's concluded that the microprocessor of the machine follows the convention of little endian. What's more, a pointer(e.g. a variable declared as being of type "double *") uses the full word size of the machine.
is needed when using cout.
// Code to print the byte representation of program objects #include "stdafx.h" #include <iostream> #include <iomanip> #include <cstdio> #include <Windows.h> using namespace std; typedef unsigned char* byte_pointer; void show_bytes(byte_pointer start, int len) { for (int i=0; i!=len; ++i) cout << " " << hex << setw(2) << setfill('0') << static_cast<int>(start[i]); //printf(" %.2x", start[i]); cout << endl; } void show_int(int x) { show_bytes((byte_pointer) &x, sizeof(int)); } void show_float(float x) { show_bytes((byte_pointer) &x, sizeof(float)); } void show_double(double x) { show_bytes((byte_pointer) &x, sizeof(double)); } void show_pointer(void* x) { show_bytes((byte_pointer) &x, sizeof(void*)); } int _tmain(int argc, _TCHAR* argv[]) { int i = 1000; show_int(i); float f = 1; show_float(f); double d = 1; show_double(d); show_pointer(&d); Sleep(5000); return 0; }
Running results on Win32 and x64 are represented on the two figures below.
Figure 1 Running results on Win32 Figure 2 Running results on x64
From the two figures, it's concluded that the microprocessor of the machine follows the convention of little endian. What's more, a pointer(e.g. a variable declared as being of type "double *") uses the full word size of the machine.
相关文章推荐
- javassit-a mean to manipulate the java bytecode of an application
- function to count the number of "1" of one byte
- Mock Objects to the Rescue! Test Your .NET Code with NMock
- How to print the content of a Rich Edit Control
- EWB使用时出错-unable to write EWB program directory please change the permissions of this directory
- 1. How to Inspect the Content of a Program Data...
- “The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 byte
- ISO 639: Code for the Representation of the Names of Languages
- 102.You want to import schema objects of the HR user using Oracle Data Pump from the development dat
- Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
- Visual Studio Code 提示"use the 'php.validate.executablePath'setting to configure the location of 'php
- An error occurred during the execution of xp_cmdshell. A call to 'LogonUserW' failed with error code: '1385'
- A Designer's Guide to Adobe InDesign and XML: Harness the Power of XML to Automate your Print and We
- [Javascript] A function works like 'print_r()' in PHP to print out the details of an object for JS debugging
- How to calculate the time consumed on a block of souce code.
- The following error occurred during schema generation: Unable to load bytecode for class
- 启动tomcat的时候,报Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program 的解决方案
- 92 Examine the output of the query that you executed to list the objects in the recycle bin: SQL> SE
- 7 Strace Examples to Debug the Execution of a Program in Linux