0

I am currently working on a python school project where I have to receive packets from tftp server and write them to a jpg file

I managed to receive all the packets and store all of them into a string where the string looks this :

"ÿØÿàÇeæuNþ¤æú;o!#ýeæÞ“7”Lê......." and so on, the size of the string is 19622.

Now what I would like to know is if there's a way to write this string to image file on disc?

and is there a better way to receive the packets into a better datatype such as dataoutputSstream for example?

Thank you,

Ben Hare
  • 4,365
  • 5
  • 27
  • 44

1 Answers1

0

Those strings are actually just the visual representation of the values of those bytes in ascii, so you should really be storing them in bytes like objects (which should be the default type you receive TCP/UDP data in). If you are literally receiving a JPG file over the network, then all you have to do write those bytes to a binary file, ala open("file_name.jpg", "wb") and make sure the new file you've created has .jpg extension. If the data is the same it will be interpreted the same as any old jpg file no matter what medium you sent it over. Its important to store them in byte arrays or the like because you want to make sure the data that you received is actually being written as the physical binary value out, and not a textual version of that string.

There is only one type of file, and that is the binary file. Even text files are binary files. But we separate the idea of "human readable" and "binary" into binary format and text formats. A python file, any .txt formats, those are all text files using this convention. A jpg, png, compiled program, or basically any code whose representation cannot be parsed via ASCii, UDP-8,16,32,64 etc, or any other textual representation is a binary file.

The thing about files is, because they are all actually binary, they are interpreted based on what you tell another program that file is (hence file extensions), and even then if the program permits, you can just ignore the extension and just tell your editor "interpret this as an X file".

Krupip
  • 4,404
  • 2
  • 32
  • 54