specifics of a binary byte

This is a discussion on specifics of a binary byte within the C++ Programming forums, part of the General Programming Boards category; hi, I am trying to do something with particular bytes in a file, be it an .exe or .txt or ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    specifics of a binary byte

    hi, I am trying to do something with particular bytes in a file, be it an .exe or .txt or any other kind, and I seem to have run into a problem. I'm using this code to get in the byte:
    Code:
    char buffer[1];
    
    ...
    
    ifstream J ("data.exe", ios::in | ios::binary);
    J.read (buffer, 1);
    but things go crazy when it converts it into a integer, or a character. Could anyone help with the conversion. IE: I know that a 'A' equals 65, and a 'a' equals 97, but what about the other bytes you would find in a .exe, and are there negative numbers for half of them? thanks

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    char goes from -127 to 127. unsigned char goes from 0 to 255
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >but things go crazy when it converts it into a integer, or a character.
    Can you be more specific?

    >char buffer[1];
    An array, size 1? Seems a bit pointless!

    Also, this might help as a reference tool:
    www.asciitable.com
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Yeah I know that the array with one entry is kind of stupid but as far as i can figure, the .read function is designed to take in strings using arrays so i figured I should just adapt to it. When I said I was trying to convert it to an int I meant that in one part of the program it looks like this:
    Code:
    char buffer[1];
    
    .....
    
    J.read (buffer, 1);
    
    .....
    
    cout<<array[buffer[0]];
    so it converts that specific byte it reads into an int to designate the array number. That is why I needed to know how the number system worked. In .exe's if you take in a char is it possible that it will be a negative? And, once again thanks.

    P.S. I voted for you hammer, because I know how important it is to us stupid newbies that someone is willing to help.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm not sure how to answer, so I've written a small prog to hex dump a file. See if this helps you understand. If not, just ask again.
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(void)
    {
        ifstream ifile;
        int c, counter = 0;
        
        ifile.open("myfile.exe", ios::binary|ios::in);
        if (!ifile)
        {  
            cout <<"Error\n";
            return EXIT_FAILURE;
        }
        
        while ((c = ifile.get()) != EOF)
        {
            cout <<hex <<c <<" ";
            if (++counter == 20)
            {
                cout <<endl;
                counter = 0;
            }
        }
        
        ifile.close();
        
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21