unpack syntax in python 3











up vote
2
down vote

favorite












I am trying to convert hex numbers into decimals using unpack.
When I use:



from struct import *
unpack("<H",b"xe2x07")


The output is: 2018, which is what I want.
The thing is I have my hex data in a list as a string in the form of:



asd = ['e2','07']


My question is is there a simple way of using unpack without the backslashes, the x? Something like so:



unpack("<H","e207")


I know this doesn't work but I hope you get the idea.



For clarification I know I could get the data in the form of b'x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.










share|improve this question




























    up vote
    2
    down vote

    favorite












    I am trying to convert hex numbers into decimals using unpack.
    When I use:



    from struct import *
    unpack("<H",b"xe2x07")


    The output is: 2018, which is what I want.
    The thing is I have my hex data in a list as a string in the form of:



    asd = ['e2','07']


    My question is is there a simple way of using unpack without the backslashes, the x? Something like so:



    unpack("<H","e207")


    I know this doesn't work but I hope you get the idea.



    For clarification I know I could get the data in the form of b'x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am trying to convert hex numbers into decimals using unpack.
      When I use:



      from struct import *
      unpack("<H",b"xe2x07")


      The output is: 2018, which is what I want.
      The thing is I have my hex data in a list as a string in the form of:



      asd = ['e2','07']


      My question is is there a simple way of using unpack without the backslashes, the x? Something like so:



      unpack("<H","e207")


      I know this doesn't work but I hope you get the idea.



      For clarification I know I could get the data in the form of b'x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.










      share|improve this question















      I am trying to convert hex numbers into decimals using unpack.
      When I use:



      from struct import *
      unpack("<H",b"xe2x07")


      The output is: 2018, which is what I want.
      The thing is I have my hex data in a list as a string in the form of:



      asd = ['e2','07']


      My question is is there a simple way of using unpack without the backslashes, the x? Something like so:



      unpack("<H","e207")


      I know this doesn't work but I hope you get the idea.



      For clarification I know I could get the data in the form of b'x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.







      python-3.x unpack






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 15:16









      Mihai Chelaru

      2,0927922




      2,0927922










      asked Nov 19 at 15:10









      James

      476




      476
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).



          >>> codecs.decode('e207', 'hex')
          b'xe2x07'


          A convenience function for the same thing:



          >>> bytes.fromhex('e207')
          b'xe2x07'


          Now you can struct.unpack those bytes. Putting it all together:



          >>> asd = ['e2','07']
          >>> text = ''.join(asd)
          >>> encoded = codecs.decode(text, 'hex')
          >>> struct.unpack("<H", encoded)
          (2018,)





          share|improve this answer























          • will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
            – James
            Nov 19 at 15:27










          • I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
            – wim
            Nov 19 at 15:33










          • what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
            – James
            Nov 20 at 12:40










          • As long as they are all 0-9 a-f then it doesn't matter.
            – wim
            Nov 20 at 14:57











          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',
          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%2f53377529%2funpack-syntax-in-python-3%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








          up vote
          1
          down vote













          You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).



          >>> codecs.decode('e207', 'hex')
          b'xe2x07'


          A convenience function for the same thing:



          >>> bytes.fromhex('e207')
          b'xe2x07'


          Now you can struct.unpack those bytes. Putting it all together:



          >>> asd = ['e2','07']
          >>> text = ''.join(asd)
          >>> encoded = codecs.decode(text, 'hex')
          >>> struct.unpack("<H", encoded)
          (2018,)





          share|improve this answer























          • will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
            – James
            Nov 19 at 15:27










          • I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
            – wim
            Nov 19 at 15:33










          • what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
            – James
            Nov 20 at 12:40










          • As long as they are all 0-9 a-f then it doesn't matter.
            – wim
            Nov 20 at 14:57















          up vote
          1
          down vote













          You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).



          >>> codecs.decode('e207', 'hex')
          b'xe2x07'


          A convenience function for the same thing:



          >>> bytes.fromhex('e207')
          b'xe2x07'


          Now you can struct.unpack those bytes. Putting it all together:



          >>> asd = ['e2','07']
          >>> text = ''.join(asd)
          >>> encoded = codecs.decode(text, 'hex')
          >>> struct.unpack("<H", encoded)
          (2018,)





          share|improve this answer























          • will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
            – James
            Nov 19 at 15:27










          • I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
            – wim
            Nov 19 at 15:33










          • what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
            – James
            Nov 20 at 12:40










          • As long as they are all 0-9 a-f then it doesn't matter.
            – wim
            Nov 20 at 14:57













          up vote
          1
          down vote










          up vote
          1
          down vote









          You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).



          >>> codecs.decode('e207', 'hex')
          b'xe2x07'


          A convenience function for the same thing:



          >>> bytes.fromhex('e207')
          b'xe2x07'


          Now you can struct.unpack those bytes. Putting it all together:



          >>> asd = ['e2','07']
          >>> text = ''.join(asd)
          >>> encoded = codecs.decode(text, 'hex')
          >>> struct.unpack("<H", encoded)
          (2018,)





          share|improve this answer














          You have hex-encoded data, in a text object. So, to go back to raw hex bytes, you can decode the text string. Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).



          >>> codecs.decode('e207', 'hex')
          b'xe2x07'


          A convenience function for the same thing:



          >>> bytes.fromhex('e207')
          b'xe2x07'


          Now you can struct.unpack those bytes. Putting it all together:



          >>> asd = ['e2','07']
          >>> text = ''.join(asd)
          >>> encoded = codecs.decode(text, 'hex')
          >>> struct.unpack("<H", encoded)
          (2018,)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 16:57

























          answered Nov 19 at 15:21









          wim

          154k47292423




          154k47292423












          • will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
            – James
            Nov 19 at 15:27










          • I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
            – wim
            Nov 19 at 15:33










          • what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
            – James
            Nov 20 at 12:40










          • As long as they are all 0-9 a-f then it doesn't matter.
            – wim
            Nov 20 at 14:57


















          • will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
            – James
            Nov 19 at 15:27










          • I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
            – wim
            Nov 19 at 15:33










          • what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
            – James
            Nov 20 at 12:40










          • As long as they are all 0-9 a-f then it doesn't matter.
            – wim
            Nov 20 at 14:57
















          will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
          – James
          Nov 19 at 15:27




          will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
          – James
          Nov 19 at 15:27












          I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
          – wim
          Nov 19 at 15:33




          I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
          – wim
          Nov 19 at 15:33












          what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
          – James
          Nov 20 at 12:40




          what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
          – James
          Nov 20 at 12:40












          As long as they are all 0-9 a-f then it doesn't matter.
          – wim
          Nov 20 at 14:57




          As long as they are all 0-9 a-f then it doesn't matter.
          – wim
          Nov 20 at 14:57


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377529%2funpack-syntax-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

          Feedback on college project

          Futebolista

          Albești (Vaslui)