Saving hexadecimal data in the database

Asked

Viewed 860 times

1

I need to register in the database a document or image in hexadecimal format and then recover these data by converting them back to correct format.

  • What exactly do you want, store it in binary or hexa? It’s originally where? File or string?

  • 1

    A remark: normally, in the practical cases of day-to-day it does not make much sense to keep working with the expanded data in hexadecimal format, but only generate them at the time of display or final use. Maybe you have a very specific case that really needs this, but it’s good to look at it. In the case of sending to printers, or to work with MAC addresses, or sending hashes and any similar tasks, it only makes sense to work with hexa in the final step of the process (display, submission, etc). Anyway, I posted an answer in this regard.

1 answer

5


PHP already has its own functions for this:

string hex2bin ( string $data )

that converts the string hehadecimal passed in parameter $data in a binary format, which can be stored in a BLOB in the database, or convert data from a Varchar to string, if you are storing in hexa but using the data in bytes ...


...and also the

string bin2hex ( string $str )

that converts the string spent in $str and returns the corresponding hexadecimal, so when you recover the data from BLOB, can convert them back to hexa, or if you are using "the other way around", you can take data in bytes and use this function to convert the data to a string hexa, which can be stored in a Varchar.


To use them, it depends on your practical case. The important thing is to know that by taking the data in hexadecimal, you can pass them to binary with hex2bin, effectively decreasing their space by 50% to store in a BLOB in the database, and while recovering DB data, just use the bin2hex to have the data in hexa again.


Examples:

<?php
   $data = '546573746520646520636f6e76657273616f';
   echo hex2bin( $data );
?>

Exit:

Teste de conversao


<?php
   $data = file_get_contents( 'teste.jpg' );
   echo bin2hex( $data );
?>

Exit:

FFD8FFE102DE45786966000049492A00080000000F000001030001000000100A...
  • It will either be a doc, xml or image document (jpeg, jpg, png). Another thing, as I am working, here in the company, with MSSQL database, Sqlserver, there is no field of type BLOB, so I am putting the field as).

  • @Gustavosevero whatever the type of information, the bin2hex solves well. Of curiosity, because you are working with them in hexa?

  • The reason is that I am developing an application in PHP and there is already this application, however, in aspx. And in this application aspx images and documents are registered in the bank in this way. I do not program in aspx.

  • @Gustavosevero was probably used to store in common strings, to avoid using BLOB. It’s usually a huge waste of space. The good thing would be if you store everything in binary, and no longer use hexadecimal from now on. (But of course, it’s just my guess). Even, of curiosity, in the visual tools that work with mysql itself you can see the binary data shown on the screen represented as hexadecimal.

  • @Gustavosevero no Sqlserver the equivalent of BLOB is Varbinary. Varchar does not serve for binary data. The good thing is that with Varbinary you don’t need Hexa either.

  • Right. But how to insert a data in the bank, photo, for example, in binary format?

  • 1

    Just insert without converting to hexadecimal (or if it is in hexadecimal already, just convert according to the answer). When I speak "binary", it is not in the sense of numerical notation, but in the original file. The file is a sequence of bytes, Varbinary accepts any sequence of bytes, without having to convert to anything. You can read the file with $dados file_get_contents() and insert into the bank with mysqli_send_long_data()

  • I get it. I thought you were talking binary notation anyway, but I get that it’s not. Thank you.

  • @Gustavosevero is that I went "on the wave" of the term used in the documentation of Varbinary and expressed myself badly. PS: The example in the comment was how to save to a Mysql database.

  • Bacco, I got it... Just like this: echo '<img src="'. $prefix. '/'. $folder. '/'. $name.'" >';

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.