0

I'm novice in python and I develop this function to retrieve coilstatus but in binary format:

 def reg_val_list1(s):
v = []
for p in s:
    if p.haslayer('ModbusADUResponse'):
        try:
            if p['ModbusADUResponse'][1].funcCode == 1:
                v += p['ModbusADUResponse'][1].coilStatus
        except AttributeError:
            pass

return v

I want to obtain a binary values instead of a decimals ones. Here is my function

This function returns me values ​​in decimal like 64,3, ... and I would like to have these same values ​​in binary ie:

0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0: 0,0,0,0,0,0,1,0 = 64 and 1,1,0,0,0,0,0,0 = 3

I modified the function by putting several solutions but none of them work. I made this change:

def reg_val_list1(s):
v = []
u = []
for p in s:
    if p.haslayer('ModbusADUResponse'):
        try:
            if p['ModbusADUResponse'][1].funcCode == 1:
                v += p['ModbusADUResponse'][1].coilStatus
                u = "{0:b}".format(v)
        except AttributeError:
            pass
   return u

I also tried this:

def reg_val_list1(s):
v = []
for p in s:
    if p.haslayer('ModbusADUResponse'):
        try:
            if p['ModbusADUResponse'][1].funcCode == 1:
                v += bin (p['ModbusADUResponse'][1].coilStatus)
        except AttributeError:
            pass
   return v

and encode() too

But nothing works. any help please ?

salwa17
  • 13
  • 6
  • 1
    Does this answer your question? [Convert base-2 binary number string to int](https://stackoverflow.com/questions/8928240/convert-base-2-binary-number-string-to-int) – Simon Crane May 20 '20 at 14:52
  • I think the question is exactly opposite - from decimal to binary, not binary to int. However, the example `0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0: 0,0,0,0,0,0,1,0 = 64` doesn't make sense - binary representation of 64 is 1000000. – Błotosmętek May 20 '20 at 14:58
  • Yeah you're right from decimal to binary. I give the bits as I retrieve them from the pcap file but you're right : 0,1,0,0,0,0,0,0 = 64 and 0,0,0,0,0,0,1,1 = 3. in the proposed forum, people propose function bin() but it doesn't work – salwa17 May 22 '20 at 12:13

0 Answers0