Encoding in 8 Bit Integer to Text in Python 3Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Converting integer to string?Does Python have a string 'contains' substring method?

May a hotel provide accommodation for fewer people than booked?

Can living where magnetic ore is abundant provide any protection from cosmic radiation?

"DDoouubbllee ssppeeaakk!!"

Went to a big 4 but got fired for underperformance in a year recently - Now every one thinks I'm pro - How to balance expectations?

Were there any unmanned expeditions to the moon that returned to Earth prior to Apollo?

IBM mainframe classic executable file formats

How to derive trigonometric Cartesian equation from parametric

Is there a general term for the items in a directory?

Should students have access to past exams or an exam bank?

Can I say "Gesundheit" if someone is coughing?

Why is “deal 6 damage” a legit phrase?

Why do MS SQL Server SEQUENCEs not have an ORDER parameter like Oracle?

A game of red and black

Why did the United States not resort to nuclear weapons in Vietnam?

Gold Battle KoTH

Adjective for when skills are not improving and I'm depressed about it

Why should I use a big powerstone instead of smaller ones?

"Will flex for food". What does this phrase mean?

Error with uppercase in titlesec's label field

How to compare files with diffrent extensions and delete extra files?

Accurately recalling the key - can everyone do it?

How did Biff return to 2015 from 1955 without a lightning strike?

Feedback diagram

How do I respond appropriately to an overseas company that obtained a visa for me without hiring me?



Encoding in 8 Bit Integer to Text in Python 3


Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Converting integer to string?Does Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I updating a script to Python 3 from Python 2 and having trouble with the line below, it decodes and displays the RSSI value of a BLE Beacon:



rssi = struct.unpack("b", packet[packetOffset -1])


I found an alternative method to get the same result, which takes the 2 last characters in the broadcast string and converts them to text. Using an 8 Bit Signed Integer Encoder.



For example c3 becomes -61 (https://cryptii.com/pipes/integer-encoder)



However I am struggling to find a way to do this in python 3!



How do I decode the string?



Any help will be much appreciated.










share|improve this question



















  • 1





    Can you give us tha string?

    – Pedro Rodrigues
    Mar 26 at 23:33











  • Did you notice that int('c3', 16) -> 195 and accidentally 195 - 256 = -61?

    – Klaus D.
    Mar 26 at 23:34

















1















I updating a script to Python 3 from Python 2 and having trouble with the line below, it decodes and displays the RSSI value of a BLE Beacon:



rssi = struct.unpack("b", packet[packetOffset -1])


I found an alternative method to get the same result, which takes the 2 last characters in the broadcast string and converts them to text. Using an 8 Bit Signed Integer Encoder.



For example c3 becomes -61 (https://cryptii.com/pipes/integer-encoder)



However I am struggling to find a way to do this in python 3!



How do I decode the string?



Any help will be much appreciated.










share|improve this question



















  • 1





    Can you give us tha string?

    – Pedro Rodrigues
    Mar 26 at 23:33











  • Did you notice that int('c3', 16) -> 195 and accidentally 195 - 256 = -61?

    – Klaus D.
    Mar 26 at 23:34













1












1








1








I updating a script to Python 3 from Python 2 and having trouble with the line below, it decodes and displays the RSSI value of a BLE Beacon:



rssi = struct.unpack("b", packet[packetOffset -1])


I found an alternative method to get the same result, which takes the 2 last characters in the broadcast string and converts them to text. Using an 8 Bit Signed Integer Encoder.



For example c3 becomes -61 (https://cryptii.com/pipes/integer-encoder)



However I am struggling to find a way to do this in python 3!



How do I decode the string?



Any help will be much appreciated.










share|improve this question














I updating a script to Python 3 from Python 2 and having trouble with the line below, it decodes and displays the RSSI value of a BLE Beacon:



rssi = struct.unpack("b", packet[packetOffset -1])


I found an alternative method to get the same result, which takes the 2 last characters in the broadcast string and converts them to text. Using an 8 Bit Signed Integer Encoder.



For example c3 becomes -61 (https://cryptii.com/pipes/integer-encoder)



However I am struggling to find a way to do this in python 3!



How do I decode the string?



Any help will be much appreciated.







python python-3.x bluetooth bluetooth-lowenergy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 23:27









tbowdentbowden

3922 silver badges18 bronze badges




3922 silver badges18 bronze badges










  • 1





    Can you give us tha string?

    – Pedro Rodrigues
    Mar 26 at 23:33











  • Did you notice that int('c3', 16) -> 195 and accidentally 195 - 256 = -61?

    – Klaus D.
    Mar 26 at 23:34












  • 1





    Can you give us tha string?

    – Pedro Rodrigues
    Mar 26 at 23:33











  • Did you notice that int('c3', 16) -> 195 and accidentally 195 - 256 = -61?

    – Klaus D.
    Mar 26 at 23:34







1




1





Can you give us tha string?

– Pedro Rodrigues
Mar 26 at 23:33





Can you give us tha string?

– Pedro Rodrigues
Mar 26 at 23:33













Did you notice that int('c3', 16) -> 195 and accidentally 195 - 256 = -61?

– Klaus D.
Mar 26 at 23:34





Did you notice that int('c3', 16) -> 195 and accidentally 195 - 256 = -61?

– Klaus D.
Mar 26 at 23:34












2 Answers
2






active

oldest

votes


















2














Is it what you are looking for?



a = bytes.fromhex('c3')

res = a[0] - 256 if a[0] > 127 else a[0]


bytes.fromhex takes a string and transforms it to a byte object. You take the first byte and make it unsigned by checking whether it's greater than 127.



Alternatively:



res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)





share|improve this answer



























  • Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?

    – tbowden
    Mar 26 at 23:53











  • There is. If you try int('0f', 16) - 256 you get -241. You need to check whether the value is >127 or not. Using int is a good idea. Not sure of the consequences. I need to think about it.

    – Jacques Gaudin
    Mar 26 at 23:57


















1














You haven't shown exactly what's in packet, but here's a guess:



import struct


packet = b'x00xc3x00xff'
packetOffset = 2

rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
print(repr(rssi)) # -> -61

# For a text string result, just do:
rssi = str(rssi)
print(repr(rssi)) # -> '-61'





share|improve this answer





























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367633%2fencoding-in-8-bit-integer-to-text-in-python-3%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    Is it what you are looking for?



    a = bytes.fromhex('c3')

    res = a[0] - 256 if a[0] > 127 else a[0]


    bytes.fromhex takes a string and transforms it to a byte object. You take the first byte and make it unsigned by checking whether it's greater than 127.



    Alternatively:



    res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)





    share|improve this answer



























    • Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?

      – tbowden
      Mar 26 at 23:53











    • There is. If you try int('0f', 16) - 256 you get -241. You need to check whether the value is >127 or not. Using int is a good idea. Not sure of the consequences. I need to think about it.

      – Jacques Gaudin
      Mar 26 at 23:57















    2














    Is it what you are looking for?



    a = bytes.fromhex('c3')

    res = a[0] - 256 if a[0] > 127 else a[0]


    bytes.fromhex takes a string and transforms it to a byte object. You take the first byte and make it unsigned by checking whether it's greater than 127.



    Alternatively:



    res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)





    share|improve this answer



























    • Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?

      – tbowden
      Mar 26 at 23:53











    • There is. If you try int('0f', 16) - 256 you get -241. You need to check whether the value is >127 or not. Using int is a good idea. Not sure of the consequences. I need to think about it.

      – Jacques Gaudin
      Mar 26 at 23:57













    2












    2








    2







    Is it what you are looking for?



    a = bytes.fromhex('c3')

    res = a[0] - 256 if a[0] > 127 else a[0]


    bytes.fromhex takes a string and transforms it to a byte object. You take the first byte and make it unsigned by checking whether it's greater than 127.



    Alternatively:



    res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)





    share|improve this answer















    Is it what you are looking for?



    a = bytes.fromhex('c3')

    res = a[0] - 256 if a[0] > 127 else a[0]


    bytes.fromhex takes a string and transforms it to a byte object. You take the first byte and make it unsigned by checking whether it's greater than 127.



    Alternatively:



    res = int.from_bytes(bytes.fromhex('c3'), byteorder='big', signed=True)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 27 at 0:21

























    answered Mar 26 at 23:49









    Jacques GaudinJacques Gaudin

    7,6883 gold badges28 silver badges51 bronze badges




    7,6883 gold badges28 silver badges51 bronze badges















    • Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?

      – tbowden
      Mar 26 at 23:53











    • There is. If you try int('0f', 16) - 256 you get -241. You need to check whether the value is >127 or not. Using int is a good idea. Not sure of the consequences. I need to think about it.

      – Jacques Gaudin
      Mar 26 at 23:57

















    • Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?

      – tbowden
      Mar 26 at 23:53











    • There is. If you try int('0f', 16) - 256 you get -241. You need to check whether the value is >127 or not. Using int is a good idea. Not sure of the consequences. I need to think about it.

      – Jacques Gaudin
      Mar 26 at 23:57
















    Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?

    – tbowden
    Mar 26 at 23:53





    Yes! Is there a big difference if I used 'num = int('c3', 16) - 256' instead?

    – tbowden
    Mar 26 at 23:53













    There is. If you try int('0f', 16) - 256 you get -241. You need to check whether the value is >127 or not. Using int is a good idea. Not sure of the consequences. I need to think about it.

    – Jacques Gaudin
    Mar 26 at 23:57





    There is. If you try int('0f', 16) - 256 you get -241. You need to check whether the value is >127 or not. Using int is a good idea. Not sure of the consequences. I need to think about it.

    – Jacques Gaudin
    Mar 26 at 23:57













    1














    You haven't shown exactly what's in packet, but here's a guess:



    import struct


    packet = b'x00xc3x00xff'
    packetOffset = 2

    rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
    print(repr(rssi)) # -> -61

    # For a text string result, just do:
    rssi = str(rssi)
    print(repr(rssi)) # -> '-61'





    share|improve this answer































      1














      You haven't shown exactly what's in packet, but here's a guess:



      import struct


      packet = b'x00xc3x00xff'
      packetOffset = 2

      rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
      print(repr(rssi)) # -> -61

      # For a text string result, just do:
      rssi = str(rssi)
      print(repr(rssi)) # -> '-61'





      share|improve this answer





























        1












        1








        1







        You haven't shown exactly what's in packet, but here's a guess:



        import struct


        packet = b'x00xc3x00xff'
        packetOffset = 2

        rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
        print(repr(rssi)) # -> -61

        # For a text string result, just do:
        rssi = str(rssi)
        print(repr(rssi)) # -> '-61'





        share|improve this answer















        You haven't shown exactly what's in packet, but here's a guess:



        import struct


        packet = b'x00xc3x00xff'
        packetOffset = 2

        rssi = struct.unpack("b", packet[packetOffset-1: packetOffset])[0]
        print(repr(rssi)) # -> -61

        # For a text string result, just do:
        rssi = str(rssi)
        print(repr(rssi)) # -> '-61'






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 27 at 0:07

























        answered Mar 27 at 0:01









        martineaumartineau

        74.3k11 gold badges101 silver badges193 bronze badges




        74.3k11 gold badges101 silver badges193 bronze badges






























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55367633%2fencoding-in-8-bit-integer-to-text-in-python-3%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현