title: C++ memcpy to c# equivalent
body: <p>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</p>
  
  <pre><code>    long hfzWriteHeader(hfzFile* fs, hfzHeader&amp; fh) {
  
          // copy header into buffer
          char HeaderBuf[28];
          long HeaderBufPos = 0;
          sprintf(HeaderBuf+HeaderBufPos, "HF2"); HeaderBufPos+=4;
          hfzMemcpy(HeaderBuf+HeaderBufPos, &amp;(fh.FileVersionNo), 2); HeaderBufPos+=2;
          hfzMemcpy(HeaderBuf+HeaderBufPos, &amp;(fh.nx), 4); HeaderBufPos+=4;
          hfzMemcpy(HeaderBuf+HeaderBufPos, &amp;(fh.ny), 4); HeaderBufPos+=4;
          hfzMemcpy(HeaderBuf+HeaderBufPos, &amp;(fh.TileSize), 2); HeaderBufPos+=2;
          hfzMemcpy(HeaderBuf+HeaderBufPos, &amp;(fh.Precis), 4); HeaderBufPos+=4;
          hfzMemcpy(HeaderBuf+HeaderBufPos, &amp;(fh.HorizScale), 4); HeaderBufPos+=4;
  
          // put extended header into a buffer
          char* pExtHeaderData = 0;
          long rval = hfzHeader_EncodeExtHeaderBuf(fh, &amp;pExtHeaderData);
          if(rval&lt;0) return rval;
  
          // now write in header length of ext header
          hfzMemcpy(HeaderBuf+HeaderBufPos, &amp;(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;
     }
  </code></pre>
  
  <p>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.</p>
  
  <p>Any help or enlightenment in my lack of C knowledge is appreciated</p>
  
  <p>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.</p>
  
  <p>========= Preliminary result ==============</p>
  
  <p>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 !</p>
  
  <p>Just for reference this is the file format I'm trying to write and convert the library for:
  <a href="http://www.bundysoft.com/docs/doku.php?id=l3dt:formats:specs:hf2" rel="nofollow">http://www.bundysoft.com/docs/doku.php?id=l3dt:formats:specs:hf2</a></p>
  
  <pre><code>    // 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);
  </code></pre>