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

Create a binary file viewer using c++. Notice the hexadecimal values being capit

ID: 674402 • Letter: C

Question

Create a binary file viewer using c++.

Notice the hexadecimal values being capitalized.

The following methods and functions might be useful for completing your program.

Methods/functions:? fstream get() method ? isprint() function

Standard IO manipulators: ? setw()

Do not use “magic numbers” for the 20 (lines) and 16 (bytes) to be shown in the program.

As a test you can try to read in the testASCII.bin file and you can test the object file created while compiling this program.

testASCII.bin file can be found here

http://www.filedropper.com/testascii

Explanation / Answer

/* C++ Program to conver hexa decimal to ASCII*/
#include <iostream>
#include <cstring>

using namespace std;

int getDigitValue(char digit) {
   int n = digit-'0';
   if (n>=0 && n<10) { return n; }
   if (digit=='A') { return 10; }
   if (digit=='B') { return 11; }
   if (digit=='C') { return 12; }
   if (digit=='D') { return 13; }
   if (digit=='E') { return 14; }
   if (digit=='F') { return 15; }
   return -1;
}

int getPairValue(char digit1, char digit2) {
   int v1 = getDigitValue(digit1);
   int v2 = getDigitValue(digit2);
   return (v1==-1 || v2==-1) ? -1 : v1 * 16 + v2;
}

bool hexStrToStr(char *dst, const char *src, int size) {
   bool isGood = false;
   if (size % 2 == 0) {
       isGood = true;
       for (int i=0; i<size; i+=2) {
           int pv = getPairValue(src[i], src[i+1]);
           if (pv==-1) { isGood = false; break; }
           *dst = pv;
           dst++;
       }
   }
   *dst = 0;
   return isGood;
}

bool hexStrToStr(char *dst, const char *src) { return hexStrToStr(dst, src, strlen(src)); }

int main() {
   char buffer[100];
   char RecvData[64] = "474F4420426C65737320416D657269636121";

   cout << RecvData << endl;
   cout << "isGood = " << hexStrToStr(buffer, RecvData) << endl;
   cout << buffer << endl;

   cout << "isGood = " << hexStrToStr(buffer, RecvData, 18) << endl;
   cout << buffer << endl;
  
   return 0;
}

OUTPUT:

-------------------------------------------------------------------------------------

474F4420426C65737320416D657269636121
isGood = 1
GOD Bless America!
isGood = 1
GOD Bless