C++ to C# Code Conversion Types -
i trying convert following c# not getting expected results.
c++ code:
dword ccskw::getcs(cstring strfile) { try { cfile file(strfile, cfile::moderead); word wtemp = 0; word wcs = 0; const uint uimaxcharread = 2000; tchar c[uimaxcharread]; uint uicharread; #ifdef _unicode while(uicharread = file.read(c, uimaxcharread)) { for(uint i=0; < uicharread; i++) wcs ^= c[0]; } #else while(uicharread = file.read(c, uimaxcharread)) { for(uint i=0; < uicharread-1; i++) { wtemp = c[i]; i++; wtemp <<= 8; wtemp += c[i]; wcs ^= wtemp; } } #endif file.close(); return (dword)wcs; } catch(cfileexception *e) { e->reporterror(); e->delete(); } return 0; }
briefly code reads value binary file , returns it. value used checksum calculation method.
my c# code:
public uint32 getcs() { try { ushort wtemp = 0; ushort wcs = 0; byte[] buffer = new byte[2000]; int count; using (filestream fs = new filestream(csfile, filemode.open)) { while ((count = fs.read(buffer, 0, buffer.length)) > 0) { (int = 0; < count - 1; i++) { wtemp = buffer[i]; i++; wtemp <<= 8; wtemp += buffer[i]; wcs ^= wtemp; } } } return convert.touint32(wcs); } catch (exception ex) { console.writeline(ex); } return 0; }
i know might difficult without knowing code, know if on right track or if have made obvious errors.
thank you!
the issue here c++ code using tchar
, signed 8-bit type, c# code using byte
, unsigned 8-bit type.
that means line of code:
wtemp += buffer[i];
will increase value of wtemp
in c# code because buffer[i]
unsigned, in c++ code decrease value of wtemp
values of buffer[i] >= 0x80
.
you can fix changing offending line to:
unchecked { wtemp = (ushort)(wtemp + (sbyte)buffer[i]); }
after doing that, sample file produce result of 0x3336
, expected.
Comments
Post a Comment