Sign in to follow this  
Followers 0
DarkBoost

JPG to Binary/ASCII/HEX then back to JPG

9 posts in this topic

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@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

Share this post


Link to post
Share on other sites

I still have not found a solution to this, has anyone else?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Worked for me too. Thank you!

I thought using Binary would only convert the String > Binary which is why I was using the StringToBinary and BinaryToString

Share this post


Link to post
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
Sign in to follow this  
Followers 0