Python Forum
binary data in source code form
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
binary data in source code form
#1
i have some big binary data i need to embed in a python source file with the intention that it will be written to a binary file.  one way to do this is to store it as a string literal with backslash encoding.  is there a more efficient way to do it?  would big integer literals be more efficient?  assume the binary data is already compressed and cannot be further compressed.  would base64 be better?  base85?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
If it's binary, don' t put it in a string. Use a byte array.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
this is Doug Hellmann's (author Python Standard Library) blog on binary nmanipulations: https://pymotw.com/3/struct/
Reply
#4
Base64 is how binary images are stored in source fairly frequently. Not sure if that helps you or not.
Reply
#5
(May-23-2017, 05:49 AM)Ofnuts Wrote: If it's binary, don' t put it in a string. Use a byte array.

that's really what i mean.  it's still written like a string.  what i am wondering is if ints could be a more efficient way to express it.  my goal is to make the file that looks like python source code as small as possible.

data = b'\000\377\377\000\000\377\377\003\000\177\177\005\006\377\377\001'
vs.

data = ( 72056494543077123, 35886981611257601 )
vs.

data = 1329207713684792564064364514848538369
it looks like int would be more efficient.

(Jun-01-2017, 09:51 PM)nilamo Wrote: Base64 is how binary images are stored in source fairly frequently.  Not sure if that helps you or not.
that is one of the first ways i looked at. but it is rather inefficient. it's better than hexadecimal, but only by 50% more. in terms of the convenience of available tools for conversion, it is a good choice. but look at how you would embed it in source code. you would have a big string of the base64 characters and the calls to convert that string. that would take up a lot of source code space.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  delivery exe without source code py loky62 2 329 Apr-04-2024, 05:47 PM
Last Post: loky62
  Help with to check an Input list data with a data read from an external source sacharyya 3 403 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Algorithm for extracting comments from Python source code Pavel1982 6 520 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Sending data from the form to DB quisatz 6 1,308 Nov-13-2023, 09:23 AM
Last Post: Annaat20
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 593 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,676 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Unable to request image from FORM Data usman 0 990 Aug-18-2022, 06:23 PM
Last Post: usman
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,046 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  Generating classes diagram from source code Pavel_47 3 5,617 Oct-01-2021, 03:31 AM
Last Post: Lou
  How do I open the Source code of a library? JaneTan 1 2,266 Aug-18-2021, 02:12 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020