-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_bytes.c
126 lines (96 loc) · 2.47 KB
/
show_bytes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
This program demonstrates using a function that
shows the hexadecimal byte values of an arbitrary object type
*/
#include<stdio.h>
//this function takes a void pointer and the size of the object in bytes
//the least significant byte is printed first
void showBytes (const void * pointer, const size_t size)
{
unsigned char * charPointer = (unsigned char*) pointer;
for(size_t i = 0; i < size; i++)
{
printf("%.2x ", *charPointer);
charPointer++;
}
printf("\n");
}
struct IntStruct
{
int a;
int b;
};
struct DifferentTypes
{
char c1;
int in;
char c2;
};
struct DifferentTypes2
{
int in;
char c1;
char c2;
};
struct DifferentTypes3
{
char c1;
char c2;
int in;
};
int main ()
{
printf("\nIn these examples, the least significant byte comes first\n\n");
const int var = 1024;
printf("Showing bytes for int 1024: \n");
showBytes(&var, sizeof(int));
printf("\n");
short sh1 = 2;
printf("Showing bytes for short 2: \n");
showBytes(&sh1, sizeof(sh1));
printf("\n");
short sh2 = -2;
printf("Showing bytes for short -2: \n");
showBytes(&sh2, sizeof(sh2));
printf("\n");
int int1 = 124;
printf("Showing bytes for int 124: \n");
showBytes(&int1, sizeof(int1));
printf("\n");
double doub1 = 0.4789;
printf("Showing bytes for double 0.4789: \n");
showBytes(&doub1, sizeof(doub1));
printf("\n");
long double ldoub1 = 0.5;
printf("Showing bytes for long double 0.5: \n");
showBytes(&ldoub1, sizeof(ldoub1));
printf("\n");
struct IntStruct mystr1;
mystr1.a = 1;
mystr1.b = 3;
printf("Showing bytes for a struct {int 1 int 3}: \n");
showBytes(&mystr1, sizeof(mystr1));
printf("\n");
struct DifferentTypes mystr2;
mystr2.c1 = 1;
mystr2.in = 2;
mystr2.c2 = 3;
printf("Showing bytes for a struct {char 1 int 2 char 3}: \n");
showBytes(&mystr2, sizeof(mystr2));
printf("\n");
struct DifferentTypes2 mystr3;
mystr3.c1 = 1;
mystr3.in = 2;
mystr3.c2 = 3;
printf("Showing bytes for a struct {int 2 char 1 char 2}: \n");
showBytes(&mystr3, sizeof(mystr3));
printf("\n");
struct DifferentTypes3 mystr4;
mystr4.c1 = 1;
mystr4.in = 2;
mystr4.c2 = 3;
printf("Showing bytes for a struct {char 1 char 3 int 2}: \n");
showBytes(&mystr4, sizeof(mystr4));
printf("\n");
return 0;
}