I've written a C++ app to translate EBCDIC to ASCII. Being fairly new to C++, I spent quite a bit of time on it, but its now working.
As part of the process, I wrote a dumping routine to output the incoming data into a hex-dump format display, like this:
Above, the first field is the hex offset into the record, then the record itself, then the eyecatcher area on the right, wrapped in "*"s.
Unpacking the data was the toughest part. For instance, converting an incoming character like 0xF2 and turning it into a string "F2". I'll show you how I did it (a cut-down version for simplicity), and would like to get some feedback on how I might have done it either more efficiently, or "better"** by leveraging (leaning) more on C++. The comments in the code and the code itself tell the story of the issue I was having when picking up a values X'80' or larger. I would be most interested simplifying this conversion process.
Thanks for your feedback. I've a thick skin, so let me have it!
I was reading up on the C++ I/O model with its Formatted I/O and the Manipulators for converting the output stream to hex, but these are only for integers, and it seems like it would be more work to do that than what I've got so far.
Thanks, Todd
** better = me writing less code
As part of the process, I wrote a dumping routine to output the incoming data into a hex-dump format display, like this:
Code:
00000000 C3F4F4F0 F1F3F0F3 F1F9F525 480CF0F0 F7F0F840 40C240F4 40000000 00914C00 *...........%H......@@.@.@.....L.*
00000020 00001CD5 D6D560C3 D6E5C5D9 C5C440C3 C8C1D9C7 C5E24000 0C00000C 00000C00 *......`.......@.......@.........*
Above, the first field is the hex offset into the record, then the record itself, then the eyecatcher area on the right, wrapped in "*"s.
Unpacking the data was the toughest part. For instance, converting an incoming character like 0xF2 and turning it into a string "F2". I'll show you how I did it (a cut-down version for simplicity), and would like to get some feedback on how I might have done it either more efficiently, or "better"** by leveraging (leaning) more on C++. The comments in the code and the code itself tell the story of the issue I was having when picking up a values X'80' or larger. I would be most interested simplifying this conversion process.
Thanks for your feedback. I've a thick skin, so let me have it!
Code:
#include <iostream>
using namespace std ;
#define SAMPLE "1 Ring To Rule Them All" // Sample string to convert
#define HEXCHARS "0123456789ABCDEF" // All valid hex chars
void to_hex(char *indata, int i) ; // Function prototype. -> data, character to convert.
int main (int argc, char * const argv[]) {
int i ;
char outdata[((sizeof SAMPLE-1) * 2)+1] ; // Double length + 1 for null terminator
outdata[sizeof outdata-1] = NULL ; // Null terminate the string
for (i = 0 ; i < sizeof SAMPLE-1 ; i++ ) { // Run the entire input string.
int c = SAMPLE[i] ; // Pick up a character
// Picking up 0X80 or above causes a negative value.
// So, add 256 to it to make it positive.
if (c < 0) c += 256 ; // make positive if picking up the byte caused the sign bit to propagate.
to_hex( &outdata[i*2], c) ; // Convert the byte just picked up
}
cout << '"' << SAMPLE << '"' << " converted is " << outdata << endl ; // Normal Text
// Now, show the scenario for a high value.
char temp[3] ;
temp[2] = NULL ;
char z = 0xFF ;
int j = (int) z ;
to_hex( temp , 0xFF ) ;
cout << "The HIGHVALUE is " << 0xFF << ". As an integer it is " << j << ". Converted it is " << temp << endl ;
return 0;
}
// Function to convert a byte to a hex displayable value.
void to_hex(char *indata, int byte) {
indata[0] = HEXCHARS[ byte / 16] ; // get left nibble
indata[1] = HEXCHARS[ byte % 16] ; // right nibble
return ;
}
I was reading up on the C++ I/O model with its Formatted I/O and the Manipulators for converting the output stream to hex, but these are only for integers, and it seems like it would be more work to do that than what I've got so far.
Thanks, Todd
** better = me writing less code