3

Is there a more or less equivalent of Pythons' struct pack in C# ? What i basically need to do is 'rewriting' a part of a python-script in C#. The following functions are required to prepare some text, so that it can be send via a socket...

def PrepString(text):
return struct.pack('>h', len(text)) + text.encode('utf_16_be')

def PrepStringMed(text):
return struct.pack('>h', len(text)) + '\x00' + '\x00'.join(list(text))

def PrepStringOld(text):
msg = '\x00' + '\x00'.join(list(text))
return chr(len(text)) + msg

I already tried some methods with some MemoryStream etc. but the remoteapplication (local server) yust spammed the log with errors... It would be nice to get this working with "Onboard"-resources of VS, yust for simplicity and whenever I would need that e.g at work, i would know how to solve it without extra-libraries

ThexBasic
  • 715
  • 1
  • 6
  • 24
  • Can you explain what that code does? I'm not familiar with Python. – Federico Berasategui Jan 14 '14 at 21:55
  • @Selman22 Damn, clicked on the wrong question. It's a duplicate of: [Struct.Pack Equivalent in C#](http://stackoverflow.com/questions/19337056/struct-pack-equivalent-in-c-sharp) – Bakuriu Jan 14 '14 at 22:00
  • @Bakuriu Sorry, if you think so... But the functions im mean are different. Also the "accepted" answer uses some additional classes created by the user, what I am not supposed/allowed to use – ThexBasic Jan 14 '14 at 22:04
  • There may be a more direct translation, but I think you're looking for something along the lines of the BinaryWriter class (http://msdn.microsoft.com/en-us/library/system.io.binarywriter(v=vs.110).aspx). You'll need to take care of endianness yourself though. – Brett Lempereur Jan 14 '14 at 23:09

0 Answers0