hello, i am attemping to write a basic swf header reader using the format specification from : http://www.the-labs.com/MacromediaFlash/SWF-Spec/SWFfileformat.html

So far i am able to read the first 3 bytes : FWS, no problem
The problem i am having is reading the version number after, it is displayed as a question mark in the command prompt.
I have tried casting the version number to an unsigned int from char but it still does not display correctly, it becomes avery large number:
Converted ? is : 429467177, and 137 as a signed char.

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
typedef unsigned char byte;

int main () {

  int length;
  char * buffer;

  ifstream is;
  is.open ("test.swf", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  //is.tellg() length = istellg() for whole file
  length = 8; //one for single bit
  is.seekg (0, ios::beg);

  // allocate memory:
  buffer = new char [length];

  // read data as a block and assig it to buffer:
  is.read (buffer,length);
  char newchar = buffer[4]+buffer[5]+buffer[6]+buffer[7];
  unsigned int Int32 = (char)newchar; //casting to integer
  //version = buffer.copy(version,1,2);


  cout.write (buffer,length); //full byte read in
  cout << "\n"<<Int32; //the type cast part

  delete[] buffer;
  return 0;


Im sure this is a casting problem, as it it stored according to the specification as an unsigned 8 bit integer. (Version UI8 Single byte file version) but i am unsure about how to cast it to its real value. Any help would be ace :)

I should ellaborate on the file im opening,
The swf file contains the following byte i am trying to read in, in the code above :

FWS||‚ p 4 ƒ@ ^CÂrfxC (^ ÿØÿÛ C

The first part are able to be read directly, as FWS.
The second part is meant to be an unsigned 8 bit integer, however when read from file only display a ? mark or an incorrectly typecasted integer as stated above.

Try printing it out as hex first:

for (int i = 0; i < length; i++)
    if (i > 0) cout << " ";
    cout << hex << (int)buffer[i];

Why do people always need to seek to get the length of a file? In this case, you want to read the header, right? How big is the header? Just create an unsigned char or byte array to read in a chunk of the file -- 512, 1024, 2048 bytes...

The line char newchar = buffer[4]+buffer[5]+buffer[6]+buffer[7]; simply adds the bytes together. What are you trying to do here?

it is displayed as a question mark in the command prompt.

Of what use is displaying a binary file at the command prompt. As rubberman suggests start by displaying the data as a series of HEX bytes. But use be sure to use them as unsigned values because bytes are neither positive nor negative.

your suggestion is helpful, and the adding the bits together was to try and collect only the bits after FWS, as that already worked i only wanted the version number located from bit 4-8 to be converted to unsigned int, but im still a rookie at following t
This article has been dead for over six months. Start a new discussion instead.