Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:18:54+00:00 2026-06-09T22:18:54+00:00

I have an array with two elements: the first is type, the second is

  • 0

I have an array with two elements: the first is type, the second is a data array I need to convert.

[['string','int32','string','int64','float32','string','string'],['any string','19','any string','198732132451654654','0.6','any string','any string']]

I know how to convert integer to binary, but in the above array with multiple types, how do I convert it to binary?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:18:56+00:00Added an answer on June 9, 2026 at 10:18 pm

    Although you’ve accepted my other answer, after noticing your comment explaining that what you really wanted was to convert the data into binary using struct.pack(), I’ve written another that might suit you even better (despite being longer and a little more complicated).

    import struct
    
    class TypeConv(object):
        BSA = '='  # native Byte order, standard Size, with no Alignment
        def __init__(self, conv_func, fmt_chr):
            self.__dict__.update(conv_func=conv_func, fmt_chr=fmt_chr)
    
        def pack(self, data):
            py_value = self.conv_func(data)
            count = str(len(py_value)) if self.conv_func is str else ''
            return struct.pack(self.BSA+count+self.fmt_chr, py_value)
    
    type_conv = {'string': TypeConv(str, 's'),
                 'int32': TypeConv(int, 'i'),
                 'int64': TypeConv(long, 'q'),
                 'float32': TypeConv(float, 'f'),
                }
    
    array = [['string', 'int32', 'string', 'int64', 'float32', 'string', 'string'],
             ['any string', '19', 'any string', '198732132451654654',
              '0.6', 'any string', 'any string']]
    
    binary_values = [type_conv[type_id].pack(data)
                         for type_id, data in zip(array[0], array[1])
                             if type_id in type_conv]  # to skip any unknown type_ids
    print binary_values
    

    Output:

    ['any string', '\x13\x00\x00\x00', 'any string', '\xfe\x9b<P\xd2\t\xc2\x02',
     '\x9a\x99\x19?', 'any string', 'any string']
    

    The TypeConv.pack() method first converts the string value to its equivalent Python value py_value, then uses struct.pack() to convert that to a C binary value — which I think is what you seek.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a array of two dimensions object[,] data; . I want to convert
I have a simple simulated array with two elements: bowl["fruit"] = "apple"; bowl["nuts"] =
I have two array of array of string [[1,'Tambaú','Praça Santo António','Tambaú','12x0',2,'I','EM',12,6,5934,50], [2,'Beira Rio','Av. Bei
Problem: I have two array where one produce a category and the second produce
I have image Array with two images out of that first image its showing
So I have trouble trying to use any of these two elements for different
I have an array with arrays of two elements. Now I would like to
I have a quadratic matrix(two-dimensional dynamic array of pointers) and need to change rows/columns
I have two cell arrays, which are mixed string/integer type, e.g.: a={'a';1;'b'} b={'b';1} I
I have two arrays (a and b) with n integer elements in the range

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.