Ok, well I have done some research on this. And I have tried a few different methods, both seem to be giving me the same results, which must mean there is something else going on here that I am not understanding, or its just not working right.

EDIT: This piece of code provided the correct results

unsigned long int version = data.at(3) << 24 | data.at(2) << 16 | data.at(1) << 8 | data.at(0);

I used the code above instead of this and it worked.....but then that will mess up my stream i believe.

unsigned long int version; reader.read(reinterpret_cast<char*>(&version),sizeof(version));

I really want to understand what is going on here :) Don't mind reading something if it helps educate me :D

#END EDIT

I have 4 bytes

50 46 48 78

They should convert to the # 1311780402

My system seems to be reporting this as the wrong value. I am originally getting 0 instead of 1311780402. Well thats just plain wrong. So Mike suggested that it maybe an issue on my compiler with Big vs Little Endian. So I tried using 2 functions to re-analyze the bytes and correct the issue.

First is

// Define for bit changing below
#define is_bigendian() ( (*(char*)&i) == 0 )

unsigned long int reverseInt(unsigned long int i) 
{
    unsigned char c1, c2, c3, c4;

    if (is_bigendian()) 
        return i;
    else 
	{
        c1 = i & 255;
        c2 = (i >> 8) & 255;
        c3 = (i >> 16) & 255;
        c4 = (i >> 24) & 255;
        return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
	}
}

version = reverseInt(version);

Second was

#include <intrin.h> // Used for endian support

version = _byteswap_ulong(version);

Both of these produced the same results (not the # I wanted) except one of them. (Going to ignore the 1 error for now).

So my question is this, are these 2 functions correct? If not why? If still not then how do i fix my problem? I am starting to think it is not 100% endian related, or there is another issue mixing with it...

Edited 5 Years Ago by MasterGberry: n/a

I tried this and got 1311780402.

#include <stdio.h>

union myu
{
	unsigned int x;
	char ch[4];
};

int main()
{
	union myu the_u;

	the_u.ch[0] = 50;
	the_u.ch[1] = 46;
	the_u.ch[2] = 48;
	the_u.ch[3] = 78;

	fprintf(stdout, "x->%u\n", the_u.x);

	return 0;
}

I don't think the stream is working right.....

std::vector<unsigned char> Enlarge(unsigned long int id, std::vector<unsigned char> data, int oldWidth, int oldHeight, int newWidth, int newHeight)
{
    std::stringstream reader;
    reader.rdbuf()->pubsetbuf(reinterpret_cast<char*>(&data[0]),data.size()); // create variable        
    // testing patch version
	std::cout << (int)data.at(0) << " " << (int)data.at(1) << " " << (int)data.at(2) << " " << (int)data.at(3) << std::endl;
	unsigned char temp[4]; reader.read(reinterpret_cast<char*>(temp), 4);
	std::cout << (int)temp[0] << " " << (int)temp[1] << " " << (int)temp[2] << " " << (int)temp[3] << std::endl;

When i display temp[] and data.at() the data.at() are the correct values, but the temp[] are all 0s. I don't think the stream is working properly at this point...

Your doing a lot of stuff on Line 4, the pointers may not be resolving properly. Try adding some more parentheses:

[B]([/B]reader.rdbuf()[B])[/B]->pubsetbuf(reinterpret_cast<char*>(&(data[0])),data.size()); // create variable

Edited 5 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.