0

This is my attempt to convert the string in to binary format:

  std ="this is the code"
  res=''.join(format(ord(i),'b')for i in test_str)
  print(" this is the conversion "+str(res))

output:

this is conversion
11101001101000110100111100111000001101001111001110000011101001101000110010110000011000111101111110010 01100101

How to do with type of file(ex.text,video,mp3 etc) in binary format?

Vega
  • 27,856
  • 27
  • 95
  • 103
  • 5
    What is your actual _goal_ here? If you could print "This MP3 file is 001010111010100101...", what would that accomplish? It's very likely that this is an [XY problem](https://meta.stackexchange.com/q/66377/248627) founded on a misunderstanding of binary data. – ChrisGPT was on strike Feb 17 '20 at 19:20
  • here i want to convert the video ,mp3 , image ,etc files in this 0 and 1 format .is it possible ? as i have done with string ! – Shrikant Sawarkar Feb 17 '20 at 19:30
  • 3
    ...right, but _why_? MP3 _is_ a binary format. What's the point of converting it into a string of 1s and 0s? – ChrisGPT was on strike Feb 17 '20 at 19:42

1 Answers1

0

I believe your question is not strictly related to Python, but for any language, as the problem relates to: What is a binary file. A binary file is just 0 and 1 (many times is read directly in hexa) for which you have to know how the structure that is saved (usually a struct) is being serialized.

For that reason, you have to know what kind of file you're reading, and have a Parser that will know the binary structure for that specific file. That's why you have libraries. Each library knows one or multiple types of files to read/write. In Linux (for example), the extension is completely irrelevant, the content of the file is the one that's important.

To be more exact for your request, some links to help you drive through How to read/process <your-file-format> in python (know that extension is not important, the file format is important!)

azbarcea
  • 3,323
  • 1
  • 20
  • 25