I am getting the correct chars when i read each individually such as data.at(0), (1)...etc. But when I am trying to read them into variables, i am getting some weird values for some of these. For instance:

Getting value of 3, i should be getting the value of 1311780402

Also something i noticed if i read some of the values later, I would use the (int) parameter to convert the char to the ASCII standard, and some of the values were negative. (For instance, should get 127, would get -128, absolute value of both adds up to 255). I believe someone said at one point that if i am trying to implement a byte[] from C# in C++, i should use an unsigned char vector instead of a char vector.

Comments?

C#

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

Read this. Then this (especially the note about sizes of integers in C++ which are dependent on the system you use).

Ok, i read both. I understand the Big-Endian and the Little-Endian approaches. So if I am understanding this correct, the read() function is using the wrong approach, so i need to have it use the other for it to use the correct reading of the bytes. I also see that I do need to use unsigned char instead of a regular char. Now my only question is how?

I suck with Bit shifting, I really don't understand it that well. If my understanding is right I just need the one set of code and to continuously use it in my code and it will fix all the variables.

Now there are 4 places in my code that are complaining about the unsigned char instead of the standard char. Should i just apply the (char*) to these 4 errors? Or is there a different approach, I am afraid if I just use the (char*) I am going to lose data.

Code for Error 1:

for (unsigned long int q = 0; q < tableCount; q++) 
	{
		for (unsigned long int j = 0; j < inTables[q].Items.size(); j++) 
		{
			// Allocate enough space for the data in your vector:
			inTables[q].Items[j].Data.resize(inTables[q].Items[j].Size);
			// Read the data into the vector directly:
			inFileDrs.read(&(inTables[q].Items[j].Data[0]), inTables[q].Items[j].Size);
		}
	}

Code for Error 2:

for (unsigned long int q = 0; q < tableCount; ++q)
	{
	    for (unsigned long int j = 0; j < outTables[q].Items.size(); ++j)
	    {
			outFileDrs.write(&(outTables[q].Items[j].Data[0]), outTables[q].Items[j].Data.size());
	    }
	}

Code for Error 3:

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(&data[0],data.size()); // create variable

Code for Error 4:

std::vector<unsigned char> newSlp(data.size()); //data.size() is probably not the right size, you should be able calculate the right size for the vector from counting all the written bytes in the code below..
    std::stringstream writer;
    writer.rdbuf()->pubsetbuf(&newSlp[0],newSlp.size());

1>AoE2Wide.cpp(1049): error C2664: 'std::basic_istream<_Elem,_Traits>::read' : cannot convert parameter 1 from 'unsigned char *' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>AoE2Wide.cpp(1102): error C2664: 'std::basic_ostream<_Elem,_Traits>::write' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>AoE2Wide.cpp(1268): error C2664: 'std::basic_streambuf<_Elem,_Traits>::pubsetbuf' : cannot convert parameter 1 from 'unsigned char *' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>AoE2Wide.cpp(1314): error C2664: 'std::basic_streambuf<_Elem,_Traits>::pubsetbuf' : cannot convert parameter 1 from 'unsigned char *' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

To overcome the errors you should do a reinterpret_cast as so:

inFileDrs.read(reinterpret_cast<char*>(&(inTables[q].Items[j].Data[0])), inTables[q].Items[j].Size);

Ok, and how do i fix the big-endian/little-endian glitch?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.