What to put hexadecimal data in a dictionary?












0















I am putting data in a dictionary. I want to classify data per source adress. My data template look like this:



(timestamp, idsource,iddest, counter)


This is an example of my data:



('1547911869', '02141592cc00000002', '02141592cc00000001', '1'), 
('1547911869', '02141592cc00000003', '02141592cc00000004', '2'),
('1547911869', '02141592cc00000004', '02141592cc00000005', '4'),


in order to put them in a dictionary, I uuse these lines:



for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
if init_time == None:
init_time = float(timestamp)

if id_src_node not in dict_node_info:
dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node)]}
else:
dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
dict_node_info[id_src_node]["Counter"].append(int(1))
dict_node_info[id_src_node]["Counter"].append(int(id_dest_node))

return dict_node_info


But this strategy gives me this error:



dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node)]}
ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'









share|improve this question


















  • 3





    What are you trying to achieve in create an integer from a hexidecimal string? if you do truely want to convert your hex string to an int then you have to tell int that its hex value. like int('FF', 16) will assign the value 255

    – Chris Doyle
    Jan 20 at 0:26


















0















I am putting data in a dictionary. I want to classify data per source adress. My data template look like this:



(timestamp, idsource,iddest, counter)


This is an example of my data:



('1547911869', '02141592cc00000002', '02141592cc00000001', '1'), 
('1547911869', '02141592cc00000003', '02141592cc00000004', '2'),
('1547911869', '02141592cc00000004', '02141592cc00000005', '4'),


in order to put them in a dictionary, I uuse these lines:



for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
if init_time == None:
init_time = float(timestamp)

if id_src_node not in dict_node_info:
dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node)]}
else:
dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
dict_node_info[id_src_node]["Counter"].append(int(1))
dict_node_info[id_src_node]["Counter"].append(int(id_dest_node))

return dict_node_info


But this strategy gives me this error:



dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node)]}
ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'









share|improve this question


















  • 3





    What are you trying to achieve in create an integer from a hexidecimal string? if you do truely want to convert your hex string to an int then you have to tell int that its hex value. like int('FF', 16) will assign the value 255

    – Chris Doyle
    Jan 20 at 0:26
















0












0








0








I am putting data in a dictionary. I want to classify data per source adress. My data template look like this:



(timestamp, idsource,iddest, counter)


This is an example of my data:



('1547911869', '02141592cc00000002', '02141592cc00000001', '1'), 
('1547911869', '02141592cc00000003', '02141592cc00000004', '2'),
('1547911869', '02141592cc00000004', '02141592cc00000005', '4'),


in order to put them in a dictionary, I uuse these lines:



for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
if init_time == None:
init_time = float(timestamp)

if id_src_node not in dict_node_info:
dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node)]}
else:
dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
dict_node_info[id_src_node]["Counter"].append(int(1))
dict_node_info[id_src_node]["Counter"].append(int(id_dest_node))

return dict_node_info


But this strategy gives me this error:



dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node)]}
ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'









share|improve this question














I am putting data in a dictionary. I want to classify data per source adress. My data template look like this:



(timestamp, idsource,iddest, counter)


This is an example of my data:



('1547911869', '02141592cc00000002', '02141592cc00000001', '1'), 
('1547911869', '02141592cc00000003', '02141592cc00000004', '2'),
('1547911869', '02141592cc00000004', '02141592cc00000005', '4'),


in order to put them in a dictionary, I uuse these lines:



for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
if init_time == None:
init_time = float(timestamp)

if id_src_node not in dict_node_info:
dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node)]}
else:
dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
dict_node_info[id_src_node]["Counter"].append(int(1))
dict_node_info[id_src_node]["Counter"].append(int(id_dest_node))

return dict_node_info


But this strategy gives me this error:



dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node)]}
ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 20 at 0:21









dinadina

1539




1539








  • 3





    What are you trying to achieve in create an integer from a hexidecimal string? if you do truely want to convert your hex string to an int then you have to tell int that its hex value. like int('FF', 16) will assign the value 255

    – Chris Doyle
    Jan 20 at 0:26
















  • 3





    What are you trying to achieve in create an integer from a hexidecimal string? if you do truely want to convert your hex string to an int then you have to tell int that its hex value. like int('FF', 16) will assign the value 255

    – Chris Doyle
    Jan 20 at 0:26










3




3





What are you trying to achieve in create an integer from a hexidecimal string? if you do truely want to convert your hex string to an int then you have to tell int that its hex value. like int('FF', 16) will assign the value 255

– Chris Doyle
Jan 20 at 0:26







What are you trying to achieve in create an integer from a hexidecimal string? if you do truely want to convert your hex string to an int then you have to tell int that its hex value. like int('FF', 16) will assign the value 255

– Chris Doyle
Jan 20 at 0:26














2 Answers
2






active

oldest

votes


















0














If you want to convert your hex inputs to ints you need to tell int that the input is in base 16, as by default it will assume base 10.



for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
if init_time == None:
init_time = float(timestamp)

if id_src_node not in dict_node_info:
dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node, 16)]}
else:
dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
dict_node_info[id_src_node]["Counter"].append(int(1))
dict_node_info[id_src_node]["Counter"].append(int(id_dest_node, 16))

return dict_node_info





share|improve this answer































    0














    You got this error because you're trying to convert an hexadecimal string into an int base 10.
    Basically your code goese through this part of code:



    >> a = '01ffff00ffffffffffffffffb00abeff'
    >> int(a)
    ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'


    So, like you can see you got a ValueError.



    So, one way to bypass this problem is to create a handy function that checks if the passed string if it's a valid int or it contains an hexadecimal.



    So, you can do something linke this:



    def to_int(elm):
    """
    Need to check if there is another exceptions may occure
    during the conversion of strings into integers
    """
    try:
    return int(elm)
    except ValueError:
    return int(elm, 16)


    for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
    if init_time == None:
    init_time = float(timestamp)

    if id_src_node not in dict_node_info:
    dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[to_int(id_dest_node)]}
    else:
    dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
    dict_node_info[id_src_node]["Counter"].append(int(1))
    dict_node_info[id_src_node]["Counter"].append(to_int(id_dest_node))

    return dict_node_info





    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%2f54272531%2fwhat-to-put-hexadecimal-data-in-a-dictionary%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









      0














      If you want to convert your hex inputs to ints you need to tell int that the input is in base 16, as by default it will assume base 10.



      for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
      if init_time == None:
      init_time = float(timestamp)

      if id_src_node not in dict_node_info:
      dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node, 16)]}
      else:
      dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
      dict_node_info[id_src_node]["Counter"].append(int(1))
      dict_node_info[id_src_node]["Counter"].append(int(id_dest_node, 16))

      return dict_node_info





      share|improve this answer




























        0














        If you want to convert your hex inputs to ints you need to tell int that the input is in base 16, as by default it will assume base 10.



        for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
        if init_time == None:
        init_time = float(timestamp)

        if id_src_node not in dict_node_info:
        dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node, 16)]}
        else:
        dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
        dict_node_info[id_src_node]["Counter"].append(int(1))
        dict_node_info[id_src_node]["Counter"].append(int(id_dest_node, 16))

        return dict_node_info





        share|improve this answer


























          0












          0








          0







          If you want to convert your hex inputs to ints you need to tell int that the input is in base 16, as by default it will assume base 10.



          for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
          if init_time == None:
          init_time = float(timestamp)

          if id_src_node not in dict_node_info:
          dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node, 16)]}
          else:
          dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
          dict_node_info[id_src_node]["Counter"].append(int(1))
          dict_node_info[id_src_node]["Counter"].append(int(id_dest_node, 16))

          return dict_node_info





          share|improve this answer













          If you want to convert your hex inputs to ints you need to tell int that the input is in base 16, as by default it will assume base 10.



          for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
          if init_time == None:
          init_time = float(timestamp)

          if id_src_node not in dict_node_info:
          dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[int(id_dest_node, 16)]}
          else:
          dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
          dict_node_info[id_src_node]["Counter"].append(int(1))
          dict_node_info[id_src_node]["Counter"].append(int(id_dest_node, 16))

          return dict_node_info






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 20 at 0:39









          Chris DoyleChris Doyle

          1,23811024




          1,23811024

























              0














              You got this error because you're trying to convert an hexadecimal string into an int base 10.
              Basically your code goese through this part of code:



              >> a = '01ffff00ffffffffffffffffb00abeff'
              >> int(a)
              ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'


              So, like you can see you got a ValueError.



              So, one way to bypass this problem is to create a handy function that checks if the passed string if it's a valid int or it contains an hexadecimal.



              So, you can do something linke this:



              def to_int(elm):
              """
              Need to check if there is another exceptions may occure
              during the conversion of strings into integers
              """
              try:
              return int(elm)
              except ValueError:
              return int(elm, 16)


              for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
              if init_time == None:
              init_time = float(timestamp)

              if id_src_node not in dict_node_info:
              dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[to_int(id_dest_node)]}
              else:
              dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
              dict_node_info[id_src_node]["Counter"].append(int(1))
              dict_node_info[id_src_node]["Counter"].append(to_int(id_dest_node))

              return dict_node_info





              share|improve this answer






























                0














                You got this error because you're trying to convert an hexadecimal string into an int base 10.
                Basically your code goese through this part of code:



                >> a = '01ffff00ffffffffffffffffb00abeff'
                >> int(a)
                ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'


                So, like you can see you got a ValueError.



                So, one way to bypass this problem is to create a handy function that checks if the passed string if it's a valid int or it contains an hexadecimal.



                So, you can do something linke this:



                def to_int(elm):
                """
                Need to check if there is another exceptions may occure
                during the conversion of strings into integers
                """
                try:
                return int(elm)
                except ValueError:
                return int(elm, 16)


                for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
                if init_time == None:
                init_time = float(timestamp)

                if id_src_node not in dict_node_info:
                dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[to_int(id_dest_node)]}
                else:
                dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
                dict_node_info[id_src_node]["Counter"].append(int(1))
                dict_node_info[id_src_node]["Counter"].append(to_int(id_dest_node))

                return dict_node_info





                share|improve this answer




























                  0












                  0








                  0







                  You got this error because you're trying to convert an hexadecimal string into an int base 10.
                  Basically your code goese through this part of code:



                  >> a = '01ffff00ffffffffffffffffb00abeff'
                  >> int(a)
                  ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'


                  So, like you can see you got a ValueError.



                  So, one way to bypass this problem is to create a handy function that checks if the passed string if it's a valid int or it contains an hexadecimal.



                  So, you can do something linke this:



                  def to_int(elm):
                  """
                  Need to check if there is another exceptions may occure
                  during the conversion of strings into integers
                  """
                  try:
                  return int(elm)
                  except ValueError:
                  return int(elm, 16)


                  for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
                  if init_time == None:
                  init_time = float(timestamp)

                  if id_src_node not in dict_node_info:
                  dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[to_int(id_dest_node)]}
                  else:
                  dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
                  dict_node_info[id_src_node]["Counter"].append(int(1))
                  dict_node_info[id_src_node]["Counter"].append(to_int(id_dest_node))

                  return dict_node_info





                  share|improve this answer















                  You got this error because you're trying to convert an hexadecimal string into an int base 10.
                  Basically your code goese through this part of code:



                  >> a = '01ffff00ffffffffffffffffb00abeff'
                  >> int(a)
                  ValueError: invalid literal for int() with base 10: '01ffff00ffffffffffffffffb00abeff'


                  So, like you can see you got a ValueError.



                  So, one way to bypass this problem is to create a handy function that checks if the passed string if it's a valid int or it contains an hexadecimal.



                  So, you can do something linke this:



                  def to_int(elm):
                  """
                  Need to check if there is another exceptions may occure
                  during the conversion of strings into integers
                  """
                  try:
                  return int(elm)
                  except ValueError:
                  return int(elm, 16)


                  for timestamp, id_src_node ,id_dest_node ,Counter in list_info:
                  if init_time == None:
                  init_time = float(timestamp)

                  if id_src_node not in dict_node_info:
                  dict_node_info[id_src_node] = {"timestamp": [float(timestamp) - init_time], 'Counter': [int(1)], 'ID_Dest':[to_int(id_dest_node)]}
                  else:
                  dict_node_info[id_src_node]["timestamp"].append(float(timestamp) - init_time)
                  dict_node_info[id_src_node]["Counter"].append(int(1))
                  dict_node_info[id_src_node]["Counter"].append(to_int(id_dest_node))

                  return dict_node_info






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 20 at 3:03

























                  answered Jan 20 at 0:42









                  Chiheb NexusChiheb Nexus

                  5,18331728




                  5,18331728






























                      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%2f54272531%2fwhat-to-put-hexadecimal-data-in-a-dictionary%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

                      Homophylophilia

                      Updating UILabel text programmatically using a function

                      Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage