1

I'm trying to port some code from a Python server that uses binascii.b2a_hex. I don't know of any C# equivalent. What function in C# would give me the same result?

I've looked up online to see if anyone has had the same question but have yet to find someone. I even looked for thirdparty libraries. I did make my application get the result from a Python script but I wanted to port the code authentically.

def send_withlen(self, data, log = True) :
    lengthstr = struct.pack(">L", len(data))
    if log :
        logging.debug(str(self.address) + ": Sent data with length - " + binascii.b2a_hex(lengthstr) + " " + binascii.b2a_hex(data))
    totaldata = lengthstr + data
    totalsent = 0
    while totalsent < len(totaldata) :
        sent = self.send(totaldata, False)
        if sent == 0:
            raise RuntimeError, "socket connection broken"
        totalsent = totalsent + sent

Here is an example of some logger debug lines showing the format of the output from the function:

2019-10-15 00:03:14,220 root         DEBUG    ('192.168.0.17', 59176): Sent data with length - 0000010a 01500a010000000000000400040000000c0000001200000004000400000005000000120000000400040000000000000003000000040004000000060000000e00000004000400000004000000110000000400040000000a000000110000000400040000000b000000626172000400040000000f0000000e0100000400040000000200000057000000040004000000030000000000000004000400000009000000666f6f0004000400000007000000626f6f000400040000000d000000666f6f0004000400000008000000fb0000000400240000000e000000015024000000000000000300040000006361631a00000003000400000063617308000000040004000000010000000e000000

2019-10-15 00:03:14,292 dirsrv       INFO     ('192.168.0.17', 59178): Sending out list of content list servers
2019-10-15 00:03:14,293 root         DEBUG    ('192.168.0.17', 59178): Sent data with length - 00000008 0001c0a800119d69
Red Duh
  • 21
  • 5
  • 1
    What exactly do you need? Some operations are super trivial depending on exactly what you are looking for. Not knowing binascii... There might be some functions for which a library is required. https://stackoverflow.com/questions/5664345/string-to-binary-in-c-sharp – Austin T French Oct 14 '19 at 22:09
  • There are several functions inside System.Text.Encoding namespace. You can get bytes from a specific encoding and viceversa. I don't know Python, could you describe what do you want to achieve? – Cleptus Oct 14 '19 at 22:45
  • Here is the logger debug line. I'm unsure of what the formatting is. You can see an example of the output from the logger in the original question now. Apologies for not including that. – Red Duh Oct 14 '19 at 23:04

0 Answers0