Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Zéro padding

    Scheduled Pinned Locked Moved Discussion
    12 Posts 2 Posters 531 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Q Offline
      quentho
      last edited by robert-hh

      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-hhR 1 Reply Last reply Reply Quote 0
      • robert-hhR Offline
        robert-hh @quentho
        last edited by

        @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))
        
        1 Reply Last reply Reply Quote 0
        • Q Offline
          quentho
          last edited by

          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

          robert-hhR 1 Reply Last reply Reply Quote 0
          • robert-hhR Offline
            robert-hh @quentho
            last edited by

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

            Q 1 Reply Last reply Reply Quote 0
            • Q Offline
              quentho @robert-hh
              last edited by

              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 ?

              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @quentho
                last edited by

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

                Q 1 Reply Last reply Reply Quote 0
                • Q Offline
                  quentho @robert-hh
                  last edited by

                  @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

                  robert-hhR 1 Reply Last reply Reply Quote 0
                  • robert-hhR Offline
                    robert-hh @quentho
                    last edited by

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

                    Q 1 Reply Last reply Reply Quote 0
                    • Q Offline
                      quentho @robert-hh
                      last edited by

                      @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.

                      robert-hhR 1 Reply Last reply Reply Quote 0
                      • robert-hhR Offline
                        robert-hh @quentho
                        last edited by

                        @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-hhR 1 Reply Last reply Reply Quote 0
                        • robert-hhR Offline
                          robert-hh @robert-hh
                          last edited by robert-hh

                          @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:]
                          Q 1 Reply Last reply Reply Quote 0
                          • Q Offline
                            quentho @robert-hh
                            last edited by

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

                            thank you for your answer

                            1 Reply Last reply Reply Quote 0

                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                            With your input, this post could be even better 💗

                            Register Login
                            • 1 / 1
                            • First post
                              Last post

                            Pycom on Twitter

                            Pycom