Jump to content

JPG to Binary/ASCII/HEX then back to JPG


Recommended Posts

I have been trying various methods of converting a JPG image to Binary/ASCII/HEX to then be used in a SQLite Table however when it is extracted out of the SQLite database it is not in its original format. I have searched the forums which touches on this topic but could not find a very simple solution eg. StringToBinary and BinaryToString looked promising however due to the conversion the file is not readable as a JPG. Any simple ideas here?

Link to comment
Share on other sites

Would it be possible for you to attach a before and after file?

by that I mean the original jpg plus the content that has been pulled from the database and dumped to a file?

Possibly some code.

It might make troubleshooting a little easier.

wtfpl-badge-1.png

Link to comment
Share on other sites

Here is an example using StringToBinary which allows the image to be included in the SQL table (without this it fails due to the unsupported characters).

I am not sure how to convert this back to the JPG format.

#include <SQLite.au3>
#include <SQLite.dll.au3>

Dim $fRead, $fOpen, $hQuery, $hRow

$fRead = StringToBinary(FileRead(@ScriptDir & "\photo.jpg"))

_SQLite_Startup()
_SQLite_Open()

_SQLite_Exec(-1, "CREATE TABLE test (photo);")
_SQLite_Exec(-1, "INSERT INTO test VALUES ('" & $fRead &"');")
_SQLite_Query(-1, "SELECT rowid, * FROM test WHERE rowid = 1;", $hQuery)
_SQLite_FetchData($hQuery, $hRow)

$fOpen = FileOpen(@ScriptDir & "\photo_new.jpg", 2)
FileWrite($fOpen, BinaryToString($hRow[1]))

_SQLite_ShutDown()

updated example to include output.

Edited by DarkBoost
Link to comment
Share on other sites

Really could have done with those before and after files...

any way try this minor mod.

#include <SQLite.au3>
  #include <SQLite.dll.au3>
  
  Dim $fRead, $fOpen, $hQuery, $hRow
  
  $fRead = Binary(FileRead(@ScriptDir & "\photo.jpg"))
  
  _SQLite_Startup()
  _SQLite_Open()
  
  _SQLite_Exec(-1, "CREATE TABLE test (photo);")
  _SQLite_Exec(-1, "INSERT INTO test VALUES ('" & $fRead &"');")
  _SQLite_Query(-1, "SELECT rowid, * FROM test WHERE rowid = 1;", $hQuery)
  _SQLite_FetchData($hQuery, $hRow)
  
  $fOpen = FileOpen(@ScriptDir & "\photo_new.jpg", 2)
  FileWrite($fOpen, Binary($hRow[1]))
  
  _SQLite_ShutDown()

Ed: scratch this checked it and its fubar.

Edited by Mobius

wtfpl-badge-1.png

Link to comment
Share on other sites

@Zedna unless i am using the function incorrectly it does the same as StringToBinary and then there is the same issue of converting it back to JPG

@Mobius it shouldn't matter which JPG you use however here is a BEFORE (left) and AFTER (right)

Posted Image

Edited by DarkBoost
Link to comment
Share on other sites

Try this.

#include <SQLite.au3>
  #include <SQLite.dll.au3>
  
  Dim $fRead, $fOpen, $hQuery, $hRow
  $hHandle = FileOpen(@ScriptDir & "\photo.jpg", 16)
  $fRead = Binary(FileRead($hHandle))
  
  _SQLite_Startup()
  _SQLite_Open()
  
  _SQLite_Exec(-1, "CREATE TABLE test (photo);")
  _SQLite_Exec(-1, "INSERT INTO test VALUES ('" & $fRead &"');")
  _SQLite_Query(-1, "SELECT rowid, * FROM test WHERE rowid = 1;", $hQuery)
  _SQLite_FetchData($hQuery, $hRow)
  
  $fOpen = FileOpen(@ScriptDir & "\photo_new.jpg", 2)
  FileWrite($fOpen, Binary($hRow[1]))
  
  _SQLite_ShutDown()
  FileClose($hHandle)
  FileClose($fOpen)

EDIT: Forgot to mention, this works on my end.

Edited by KentonBomb
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...