Problem with array of bytes

This is a discussion on Problem with array of bytes within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a strange behavior with an array of bytes which is loaded from a file. Code: char* buffer; ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    6

    Unhappy Problem with array of bytes

    Hello,

    I have a strange behavior with an array of bytes which is loaded from a file.

    Code:
        char* buffer;
    
        ifstream file ("data.bin", ios::in|ios::binary|ios::ate);
        size = file.tellg();
        file.seekg (0, ios::beg);
        buffer = new char [size];
        file.read (buffer, size);
        file.close();
        
        cout << " Filesize : " << dec << size << endl;
        
        
        for (int i = 1,j = size -1; i < 130 ; i++, j--)
        {
            cout << "---------------------------------------" << endl;
            cout << " I value: " << dec << i << endl;
            cout << " J = " << dec << j << "   J-1 = " << dec << j - 1 << endl;
            cout << "Current Char: " << hex << (int)buffer[j] << endl;
            cout << "Previous Char: " << hex << (int)buffer[j - 1] << endl;
        }
        delete[] buffer;
        system("PAUSE");
        return 0;
    It works well up to i=126 after that it messes things up

    ---------------------------------------
    I value: 123
    J = 70353 J-1 = 70352
    Current Char: 2d
    Previous Char: 64
    ---------------------------------------
    I value: 124
    J = 70352 J-1 = 70351
    Current Char: 64
    Previous Char: 75
    ---------------------------------------
    I value: 125
    J = 70351 J-1 = 70350
    Current Char: 75
    Previous Char: 74
    ---------------------------------------
    I value: 126
    J = 70350 J-1 = 70349
    Current Char: 74
    Previous Char: 3b
    ---------------------------------------
    I value: 127
    J = 70349 J-1 = 70348
    Current Char: 3b
    Previous Char: ffffffa2
    ---------------------------------------
    I value: 128
    J = 70348 J-1 = 70347
    Current Char: ffffffa2
    Previous Char: ffffffed
    ---------------------------------------
    I value: 129
    J = 70347 J-1 = 70346
    Current Char: ffffffed
    Previous Char: ffffffee
    For i = 127 I want it to return A2 , not "ffffffa2"

    Do you see any obvious mistake here?

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    --SOLVED--

    a temp var actually solved it:

    Code:
    unsigned char uprev,ucurrent;
    
    uprev = buffer[j -1];
    ucurrent = buffer[j];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve this problem using array..
    By juncas17 in forum C Programming
    Replies: 4
    Last Post: 10-12-2009, 10:19 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  4. Replies: 3
    Last Post: 11-10-2004, 12:31 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 03:28 PM

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