I'm actually a java developer, but for a lesiure project I'm trying to convert a lib from c++ to c#. I have a small background in ansi C but this code is confusing me
long hfzWriteHeader(hfzFile* fs, hfzHeader& fh) {
// copy header into buffer
char HeaderBuf[28];
long HeaderBufPos = 0;
sprintf(HeaderBuf+HeaderBufPos, "HF2"); HeaderBufPos+=4;
hfzMemcpy(HeaderBuf+HeaderBufPos, &(fh.FileVersionNo), 2); HeaderBufPos+=2;
hfzMemcpy(HeaderBuf+HeaderBufPos, &(fh.nx), 4); HeaderBufPos+=4;
hfzMemcpy(HeaderBuf+HeaderBufPos, &(fh.ny), 4); HeaderBufPos+=4;
hfzMemcpy(HeaderBuf+HeaderBufPos, &(fh.TileSize), 2); HeaderBufPos+=2;
hfzMemcpy(HeaderBuf+HeaderBufPos, &(fh.Precis), 4); HeaderBufPos+=4;
hfzMemcpy(HeaderBuf+HeaderBufPos, &(fh.HorizScale), 4); HeaderBufPos+=4;
// put extended header into a buffer
char* pExtHeaderData = 0;
long rval = hfzHeader_EncodeExtHeaderBuf(fh, &pExtHeaderData);
if(rval<0) return rval;
// now write in header length of ext header
hfzMemcpy(HeaderBuf+HeaderBufPos, &(fh.ExtHeaderLength), 4);
HeaderBufPos+=4;
// swap byte order if required (files use little endian)
if(LIBHFZ_BYTEORDER_BIGENDIAN==hfzByteOrder) {
HeaderBufPos = 4; // skip "HF2\0" string
hfzByteSwap(HeaderBuf+HeaderBufPos, 2); HeaderBufPos+=2; // FileVersionNo
hfzByteSwap(HeaderBuf+HeaderBufPos, 4); HeaderBufPos+=4; // nx
hfzByteSwap(HeaderBuf+HeaderBufPos, 4); HeaderBufPos+=4; // ny
hfzByteSwap(HeaderBuf+HeaderBufPos, 2); HeaderBufPos+=2; // TileSize
hfzByteSwap(HeaderBuf+HeaderBufPos, 4); HeaderBufPos+=4; // Precis
hfzByteSwap(HeaderBuf+HeaderBufPos, 4); HeaderBufPos+=4; // HorizScale
hfzByteSwap(HeaderBuf+HeaderBufPos, 4); HeaderBufPos+=4; // ExtHeaderLength
}
// write header
if(28!=hfzWrite(fs, HeaderBuf, 28))
return LIBHFZ_ERROR_WRITE_HEADER_FAILED;
// write extended header
if(pExtHeaderData) {
if(fh.ExtHeaderLength!=hfzWrite(fs, pExtHeaderData, fh.ExtHeaderLength)) {
hfzFree(pExtHeaderData);
return LIBHFZ_ERROR_WRITE_EXTHEAD_FAILED;
}
hfzFree(pExtHeaderData);
}
return LIBHFZ_STATUS_OK;
}
I believe it is trying to build up a char array (string ?) in memory so he can write this later to a binary file. This is the header part of the filetype. In java I would probably just use a string builder and then append away. However I do believe some byteaction is going on that each part of the string is exactly that amount of bytes , but I wonder with fh.nx that is a short what the result would be if the short is just value 1 ? is it " 1" or "0001". This is the part that is confusing me.
Any help or enlightenment in my lack of C knowledge is appreciated
it seems it would have been better to put the entire function here instead of just parts of it cause later on something is happening with the BIGENDIAN situation. Sorry for the formatting but getting it right on stackoverflow is nerve wrecking.
========= Preliminary result ==============
So I ended up using John Skeet's class to take care of the endian and came up with this ... I'm just wondering though that I'm not going about it all wrong. I'm rewriting based on the c++ method structure, but a lot of the methods are so low level , that they are somewhere available in the higher level C# ... Now I remember why I went to Java instead of C. I'm to quickly confused with all the pointer and memory handlings !
Just for reference this is the file format I'm trying to write and convert the library for: http://www.bundysoft.com/docs/doku.php?id=l3dt:formats:specs:hf2
// copy header into buffer
MiscUtil.Conversion.EndianBitConverter endian = MiscUtil.Conversion.EndianBitConverter.Big;
if (BitConverter.IsLittleEndian)
{
endian = MiscUtil.Conversion.EndianBitConverter.Little;
}
MemoryStream buffer = new MemoryStream();
EndianBinaryWriter writer = new EndianBinaryWriter(endian, buffer);
writer.Write("HF2");
writer.Write(fh.FileVersionNo);
writer.Write(fh.nx);
writer.Write(fh.ny);
writer.Write(fh.TileSize);
writer.Write(fh.Precis);
writer.Write(fh.HorizScale);