Zéro padding



  • Hello,

    would it be possible to make a padding of 0 on a binary number.
    I explain, I have 0b110 and I would like to have all the time a number on 4 bits is 0b0110. I tried this code it works but we delete directly the 0 seen as useless
    num_count : count my bit number previously

    def four_bit(num):
            k=tools.num_count(num)
            while k <5:
                num1= 0<<(k | num)
                print(num1)
                k=tools.num_count(num)
    

    Thank you



  • @robert-hh I did a concatenation with string and it's working.

    thank you for your answer



  • @robert-hh You may also add a dummy nibble upfront, such that the leading zero bits are not suppressed. like:

    frame = 0x0f
    for item in range(num_sensors):
        frame = frame << 14 | (item[0] & 0xf) << 10 | item[1] & 0x3ff
    hexframe = hex(frame)[3:]


  • @quentho For a easy conversion 14 bits per subframe looks a little uncomfortable, but let's make a assumption, that:

    • num_sensors is the numers of sensors
    • sensor_list is a list with pairs of sensor.id and sensor.value. It could as well be separate lists, of just the list with values, if the sensors ar just numbered.

    Then you can create a bit frame with:

    frame = 0
    for item in range(num_sensors):
        frame = frame << 14 | (item[0] & 0xf) << 10 | item[1] & 0x3ff
    hexframe = hex(frame)
    

    That makes use of the fact, that integer numbers in Python can have arbitrary length. Using 4 + 12 bit for ID and data is a little bit more convenient, because then sub-frames would not cross byte boundaries, and the first nibble could be read as the sensor ID.



  • @robert-hh non the total frame lengh is between 14 and 120 bits. Because i have a lot of sensor, and the sensor size is differente.



  • @quentho So the total frame length is 56 bit (4 + 10) * 4



  • @robert-hh said in Zéro padding:

    How should the frame look like, and what kind of data format do you get from the sensor?

    the frame looks like :
    0bPID1/sensorValue1/PID2/sensorValue2/PID3/sensorValue3/PID4/sensorValue4/

    le PID is on 4 bits : so i have a probleme for a PID 0b0001 for exemple

    the sensor value it's an interger of 10 bits.
    I would like to send this trame via sigfox in hexa

    thank you



  • @quentho How should the frame look like, and what kind of data format do you get from the sensor?



  • I would like to make a binary frame with what I receive from my sensors. My problem is that for my sensors I constantly lose the first zeros. Displaying it in a print is useless. And the internal zero I can not get them back. So I'm seeing to go with the strings but I do not find it optimal. Do you have a solution ?



  • @quentho I do not understand, what you want to get. Could you detail what you want to achieve?



  • I would like with only 0b0110. So i need to set the word length but when i will concatenate my bit number my first 0 will it still be there ? and how can i set the word lengh.

    Thank you for your answer



  • @quentho The internal representation has always the leading zeroes according to the word length. If you are talking about the external representation, e.g. when printing them, yoo can force leading zeroes to be printed. E.g. with:

    num = 0b110
    print( "{:04b}".format(num))
    

Log in to reply
 

Pycom on Twitter