Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ code - Byte order programming The C++ language Bit-Wise operations give you

ID: 3870934 • Letter: C

Question

C++ code - Byte order programming

The C++ language Bit-Wise operations give you the ability to work with endian.

******************************************************************

Turn in code, screen prints of successful execution

and write up the uses of Byte programming.

/* $begin show-bytes */

#include <stdio.h>

/* $end show-bytes */

#include <stdlib.h>

#include <string.h>

/* $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(" ");

}

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_bytes_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(" For x = %d ", x);

show_int(x);

show_float(f);

x = 3510593;

f = (float) x;

printf(" For x = %d ", 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(" calling test_show_bytes ");

test_show_bytes(val);

}

else

{

printf(" calling show two comp function ");

show_twocomp();

printf(" Calling simple show a function ");

simple_show_a();

printf(" Calling simple show b function ");

simple_show_b();

printf(" Calling float eg function ");

float_eg();

printf(" Calling string ueg function ");

string_ueg();

printf(" Calling string leg function ");

string_leg();

}

system("pause"); // For PC Visual Studios Only

return 0;

}

***************************************************************

Error shown on my computer:

const char s"ABCDEF" show_bytes ( (byte_pointer) s, strlen (s)) /* Send show-ustring Implicit conversion loses integer precision: 'size t' (aka 'unsigned long') to 'int' void string leg() f /* $begin show-lstring/ const char *s "abcdef"; show_bytes ((byte_pointer) s, strlen(s)); /* Send show-lstring / Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' 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) f if (argc > 1) f val strtol(argv [1], NULL, 0); Implicit conversion loses integer precision: 'long' to 'int' = printf("ncalling test_show_bytes ") test_show_bytes (val);

Explanation / Answer

This problem arises because the string function return values who range in return value varies from int.In case of strlen function the return type in size_t which is similar to unsigned int , the range of which is different from range of int.The complier can't ignore such as these may lead to lose in precision of data.In your case as that doesn't matter that much , you can use static type casting which tells the compiler that in short to trust you as a programmer and ignore any such details.

Static type casting can be done as ,

str = "ABCDEF";

p = strlen(str);
int plen = static_cast<int>(p);

This can also be used to silent off the other warning as it is due to the very same reason.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote