Python3 Convert bytes object to int2019 Community Moderator ElectionHow do I read / convert an InputStream into a String in Java?How do I parse a string to a float or int in Python?Converting string into datetimeHow do I get a consistent byte representation of strings in C# without manually specifying an encoding?Convert bytes to a string?Get int value from enum in C#What is the difference between const int*, const int * const, and int const *?Convert JS object to JSON stringHow do I convert a String to an int in Java?Easiest way to convert int to string in C++

Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?

Is having access to past exams cheating and, if yes, could it be proven just by a good grade?

Reverse string, can I make it faster?

Do Bugbears' arms literally get longer when it's their turn?

Why does the negative sign arise in this thermodynamic relation?

Am I not good enough for you?

In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?

What is the likely impact of grounding an entire aircraft series?

What wound would be of little consequence to a biped but terrible for a quadruped?

What Happens when Passenger Refuses to Fly Boeing 737 Max?

Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?

Should QA ask requirements to developers?

How could our ancestors have domesticated a solitary predator?

Single word request: Harming the benefactor

Do I really need to have a scientific explanation for my premise?

Is it true that real estate prices mainly go up?

Make a transparent 448*448 image

Are babies of evil humanoid species inherently evil?

Fourth person (in Slavey language)

Low budget alien movie about the Earth being cooked

If the Captain's screens are out, does he switch seats with the co-pilot?

How do I deal with a powergamer in a game full of beginners in a school club?

How do I express some one as a black person?

Algorithm to convert a fixed-length string to the smallest possible collision-free representation?



Python3 Convert bytes object to int



2019 Community Moderator ElectionHow do I read / convert an InputStream into a String in Java?How do I parse a string to a float or int in Python?Converting string into datetimeHow do I get a consistent byte representation of strings in C# without manually specifying an encoding?Convert bytes to a string?Get int value from enum in C#What is the difference between const int*, const int * const, and int const *?Convert JS object to JSON stringHow do I convert a String to an int in Java?Easiest way to convert int to string in C++










0















I have a bytes objects that I received from a socket and I want to extract the integer value it contains.



Looks like this



input = b'1 x00x00x00x00x00x00x00x00x00x00'


I tried



tmp_str = input.decode('ascii').strip()
int(tmp_str)


The error:



ValueError: invalid literal for int() with base 10: '1 x00x00x00x00x00x00x00x00x00x00'


However, the type of tmp_str is 'str', but the length is 20.. Looks like the object is not changed, but just some representations of it has changed..



>>> print(tmp_str)
1
>>> len (tmp_str)
20
>>> type(tmp_str)
<class 'str'>
>>> type(input)
<class 'bytes'>


How can I extract the int from this?










share|improve this question


























    0















    I have a bytes objects that I received from a socket and I want to extract the integer value it contains.



    Looks like this



    input = b'1 x00x00x00x00x00x00x00x00x00x00'


    I tried



    tmp_str = input.decode('ascii').strip()
    int(tmp_str)


    The error:



    ValueError: invalid literal for int() with base 10: '1 x00x00x00x00x00x00x00x00x00x00'


    However, the type of tmp_str is 'str', but the length is 20.. Looks like the object is not changed, but just some representations of it has changed..



    >>> print(tmp_str)
    1
    >>> len (tmp_str)
    20
    >>> type(tmp_str)
    <class 'str'>
    >>> type(input)
    <class 'bytes'>


    How can I extract the int from this?










    share|improve this question
























      0












      0








      0








      I have a bytes objects that I received from a socket and I want to extract the integer value it contains.



      Looks like this



      input = b'1 x00x00x00x00x00x00x00x00x00x00'


      I tried



      tmp_str = input.decode('ascii').strip()
      int(tmp_str)


      The error:



      ValueError: invalid literal for int() with base 10: '1 x00x00x00x00x00x00x00x00x00x00'


      However, the type of tmp_str is 'str', but the length is 20.. Looks like the object is not changed, but just some representations of it has changed..



      >>> print(tmp_str)
      1
      >>> len (tmp_str)
      20
      >>> type(tmp_str)
      <class 'str'>
      >>> type(input)
      <class 'bytes'>


      How can I extract the int from this?










      share|improve this question














      I have a bytes objects that I received from a socket and I want to extract the integer value it contains.



      Looks like this



      input = b'1 x00x00x00x00x00x00x00x00x00x00'


      I tried



      tmp_str = input.decode('ascii').strip()
      int(tmp_str)


      The error:



      ValueError: invalid literal for int() with base 10: '1 x00x00x00x00x00x00x00x00x00x00'


      However, the type of tmp_str is 'str', but the length is 20.. Looks like the object is not changed, but just some representations of it has changed..



      >>> print(tmp_str)
      1
      >>> len (tmp_str)
      20
      >>> type(tmp_str)
      <class 'str'>
      >>> type(input)
      <class 'bytes'>


      How can I extract the int from this?







      python python-3.x string int






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 6 at 16:18









      Sam DanielSam Daniel

      587415




      587415






















          1 Answer
          1






          active

          oldest

          votes


















          3














          str.strip() and bytes.strip() will not remove NUL bytes unless you tell them to explictly, as NUL bytes are not whitespace.



          You don't have to decode the bytes to str, however, as int() can accept bytes objects directly. Just call bytes.strip() and tell it to remove both spaces and NUL:



          int(input.strip(b' x00')


          Demo:



          >>> input = b'1 x00x00x00x00x00x00x00x00x00x00'
          >>> int(input.strip(b' x00'))
          1





          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%2f55027677%2fpython3-convert-bytes-object-to-int%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            str.strip() and bytes.strip() will not remove NUL bytes unless you tell them to explictly, as NUL bytes are not whitespace.



            You don't have to decode the bytes to str, however, as int() can accept bytes objects directly. Just call bytes.strip() and tell it to remove both spaces and NUL:



            int(input.strip(b' x00')


            Demo:



            >>> input = b'1 x00x00x00x00x00x00x00x00x00x00'
            >>> int(input.strip(b' x00'))
            1





            share|improve this answer



























              3














              str.strip() and bytes.strip() will not remove NUL bytes unless you tell them to explictly, as NUL bytes are not whitespace.



              You don't have to decode the bytes to str, however, as int() can accept bytes objects directly. Just call bytes.strip() and tell it to remove both spaces and NUL:



              int(input.strip(b' x00')


              Demo:



              >>> input = b'1 x00x00x00x00x00x00x00x00x00x00'
              >>> int(input.strip(b' x00'))
              1





              share|improve this answer

























                3












                3








                3







                str.strip() and bytes.strip() will not remove NUL bytes unless you tell them to explictly, as NUL bytes are not whitespace.



                You don't have to decode the bytes to str, however, as int() can accept bytes objects directly. Just call bytes.strip() and tell it to remove both spaces and NUL:



                int(input.strip(b' x00')


                Demo:



                >>> input = b'1 x00x00x00x00x00x00x00x00x00x00'
                >>> int(input.strip(b' x00'))
                1





                share|improve this answer













                str.strip() and bytes.strip() will not remove NUL bytes unless you tell them to explictly, as NUL bytes are not whitespace.



                You don't have to decode the bytes to str, however, as int() can accept bytes objects directly. Just call bytes.strip() and tell it to remove both spaces and NUL:



                int(input.strip(b' x00')


                Demo:



                >>> input = b'1 x00x00x00x00x00x00x00x00x00x00'
                >>> int(input.strip(b' x00'))
                1






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 6 at 16:21









                Martijn PietersMartijn Pieters

                719k14025092319




                719k14025092319





























                    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%2f55027677%2fpython3-convert-bytes-object-to-int%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

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко