Question
Turn in code, screen prints of successful execution. (MUST HAVE) and write up the uses of Byte programming. This is the code: Program to show
Turn in code, screen prints of successful execution. (MUST HAVE)
and write up the uses of Byte programming.
This is the code:
Program to show byte order.
Run the code
In class assignme
nt
The C++ language Bit
-
Wise
operations give
you the ability to work with endian.
//
BYTE ORDER PROGRAM
.cpp : Defines the entry point for the console application.
#include
"stdafx.h"
// PC Visual Studios only. Other comment out
/* $begin show
-
bytes */
#include
/* $end show
-
bytes */
#include
#include
/* $begin show
-
bytes */
typedef
unsigned
char
*byte_pointer;
void
show_bytes(byte_pointer start,
int
len) {
int
i;
for
(i = 0; i < len; i++)
printf(
" %.2x"
, start[i]);
//line:data:show_bytes_printf
printf(
"
\
n"
);
}
void
show_int(
int
x) {
show_bytes((byte_pointer) &x,
sizeof
(
int
));
//line:data:show_bytes_amp1
}
void
show_float(
float
x) {
show_bytes((byte_pointer) &x,
sizeof
(
float
));
//line:data:show_by
tes_amp2
}
void
show_pointer(
void
*x) {
show_bytes((byte_pointer) &x,
sizeof
(
void
*));
//line:data:show_bytes_amp3
}
/* $end show
-
bytes */
/* $begin test
-
show
-
bytes */
void
test_show_bytes(
int
val) {
int
ival = val;
float
fval = (
float
) ival;
int
*pval = &ival;
show_int(ival);
show_float(fval);
show_pointer(pval);
}
/* $end test
-
show
-
bytes */
void
simple_show_a() {
/* $begin simple
-
show
-
a */
int
val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1);
/* A. */
show_bytes(valp, 2);
/* B. */
show_bytes(valp, 3);
/* C. */
/* $end simple
-
show
-
a */
}
void
simple_show_b() {
/* $begin simple
-
show
-
b */
int
val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1);
/* A. */
show_bytes(valp, 2);
/* B. */
show_bytes(valp, 3);
/* C. */
/* $end simple
-
show
-
b */
}
void
float_eg() {
int
x = 3490593;
float
f = (
float
) x;
printf(
"
\
n For x = %d
\
n
\
n"
, x);
show_int(x);
show_float(f);
x = 3510593;
f = (
float
) x;
printf(
"
\
n
For x = %d
\
n
\
n"
, x);
show_int(x);
show_float(f);
}
void
string_ueg() {
/* $begin show
-
ustring */
const
char
*s =
"ABCDEF"
;
show_bytes((byte_pointer) s, strlen(s));
/* $end show
-
ustring */
}
void
string_leg() {
/* $begin show
-
lstring */
const
char
*s =
"abcdef"
;
show_bytes((byte_pointer) s, strlen(s));
/* $end show
-
lstring */
}
void
show_twocomp()
{
/* $begin show
-
twocomp */
short
x = 12345;
short
mx =
-
x;
show_bytes((byte_pointer) &x,
sizeof
(
short
));
show_bytes((byte_pointer) &
mx,
sizeof
(
short
));
/* $end show
-
twocomp */
}
int
main(
int
argc,
char
*argv[])
{
int
val = 12345;
if
(argc > 1) {
if
(argc > 1) {
val = strtol(argv[1], NULL, 0);
}
printf(
"
\
ncalling test_show_bytes
\
n
\
n"
);
test_show_bytes(val);
}
else
{
printf(
"
\
ncalling show two comp function
\
n
\
n"
);
show_twocomp();
printf(
"
\
nCalling simple show a function
\
n
\
n"
);
simple_show_a();
printf(
"
\
nCalling simple show b function
\
n
\
n"
);
simple_show_b();
printf(
"
\
nCalling float eg function
\
n
\
n"
);
float_eg();
printf(
"
\
nCalling string ueg function
\
n
\
n"
);
string_ueg();
printf(
"
\
nCalling string leg function
\
n
\
n"
);
string_leg();
}
system(
"pause"
);
// For PC Visual Studios Only
return
0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started