Hello. There are 2 parts of the project. Part 1 is written in python, it should translate the string to hex / blob / binary (I don’t know which is better for storing hex). And writes it to mysql. Function by which str -> hex

def strbin(s): return ''.join(format(ord(i),'0>8b') for i in s) 

The field in the table so far has the type LONGBLOB (can you tell me which is better) And so the next part on php.

  $mysqli->query("SELECT CONVERT(response USING utf8) FROM tablename WHERE id = 1 INTO OUTFILE '/tmp/test.txt'"); 

But the trouble is, the output is hex or just some strange symbols. I still do not understand where I was wrong, please tell me.

  • you do not need a byte string ( str type ( bytes on Python 3)) to convert to put in a database (remarkably binary data (blob) are able to store databases). If the input text (in Python, use the unicode type ( str on Python 3)), then again you do not need to convert to the "01" string - mysql can store (BMP) Unicode characters directly. - jfs
  • similar question: blob / hex / binary to str python - jfs

1 answer 1

I don’t know if you can do it in pure SQL (MySQL), but with PHP you can try this:

 $res = $mysqli->query("SELECT response FROM tablename WHERE id = 1"); $obj = $res->fetch_object(); $symbs = str_split($obj->response, 8); $toWrite = "" foreach ($symbs as $symb) { $toWrite .= chr(bindec($symb)); } // Write to the file. 

Performed the action of the mirror python.

  • Thank! Exactly what is needed! - LorDo 3:04 pm