I would like to read from a file and produce decimal values that are between 0 and 255 to produce an eight bit binary number. I have discovered a way to do this, but is there a better way? I am getting a char value and converting it to an int value + 128. Thanks
There is always another way.
Your code looks pretty simple. (a good thing)
The only thing I see right now is that if argc < 2 it looks like it will still try and run the rest of the code. (not good)
I haven't ran it but you set the value of lowest and highest to 127.
To me that seems wrong. One or the other will (in my thinking) always be true. (incorrectly)
// comments in your code would help me know what your trying to achieve in each step. (missing)
I acted on your suggestion and changed argc < 2 to argc != 2. It seems to work. I tried to place comments before the #include statements at the start of the file. My compiler generated multiple pages of errors. I have included lots of block quotes in this version.
#include <iostream>
#include <fstream>
usingnamespace std;
/* compiled using g++ (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4
This program checks that there are two arguments in the command line
The second argument is the name of the file that is to be read.
The file comes in as a stream of char variables that are converted
to int variables. These seem to be signed integers (-128 to 127), so
128 is added to obtain a range of 0 to 255. Eventually the decimal
value will be converted to an eight bit binary number and loaded
into an 8x8 array. This array will be transposed (column to row)
onto a second 8x8 array. The horizontal binary numbers in the
second array will be converted to decimal, and then to a char
variable and written to a file
*/
/* The variable int value holds the decimal value (hopefully it is
between 0 and 255)
The variable int lowest holds the value of the lower end of the
range of the decimal value of the stream (it should be 0). Why did
I set it to 127? I figured it was halfway between 0 and 255
The variable int highest holds the value of the higher end of the
range of the decimal value of the stream
The variable char letter holds the value of the character being
read from the file
*/
int value;
int lowest = 127;
int highest =127;
char letter;
/* When the main method is run the number of arguments is compared to
2 if the result is false then an error message is displayed
otherwise the program continues.
*/
int main(int argc, constchar *argv[])
{
if (argc != 2) {
cerr << "Usage: " << argv[0] << "<filename>\n";
return 1;
}
/* The input stream is declared and the while loop continues until
the end of file is reached. The char variable letter assumes the
value of the incoming character, and the char variable is converted
to an int variable value (signed integer -128 to 127). 128 is added
to the int variable value to hopefully get a range of 0 to 255.
*/
ifstream in(argv[1], ios::binary);
while (in) {
in.get(letter);
value = int(letter) + 128;
/* These two if statements are used to determine the range of the
decimal values. The results are then displayed
*/
if(lowest > value)
{
lowest = value;
}
if(highest < value)
{
highest = value;
}
}
cout << "lowest = " << lowest << " highest = " << highest << endl;
in.close();
return 0;
}