HexToBinary?

1. Senior Member
Join Date
Dec 1969
Posts
273

## HexToBinary?

Hello,<BR>I&#039;ve been using this function:<BR><BR>Function BinaryToHex(Binary)<BR> Dim c1, Out, OneByte<BR> <BR> &#039;For each source byte<BR> For c1 = 1 To LenB(Binary)<BR> &#039;Get the byte As hex<BR> OneByte = Hex(AscB(MidB(Binary, c1, 1)))<BR> <BR> &#039;append zero For bytes &#060; 0x10<BR> If Len(OneByte) = 1 Then OneByte = "0" & OneByte<BR> <BR> &#039;join the byte To OutPut stream<BR> Out = Out & OneByte<BR> Next<BR> <BR> &#039;Set OutPut value<BR> BinaryToHex = Out<BR>End Function<BR><BR>to convert binary to hex and then store it in my data base,<BR>only small files though, like under 50k.<BR>and i&#039;ve got it to work,<BR>but now that i want to show the images that are stored in the data base i have to convert it back to binary from Hex.<BR><BR>can any one show me how to kind of reverse this code to make the hex back into binary code??<BR><BR>thanks in advance for your time!

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Easy enough...

Function HexToBinary(hexText)<BR> binary = ""<BR> For bytenum = 1 To Len(hexText) Step 2<BR> oneByte = CInt("&H" & Mid(hexText,bytenum,2) )<BR> binary = binary & ChrB(oneByte)<BR> Next<BR>End Function<BR><BR>*********<BR><BR>HOWEVER...I&#039; m not sure that works because I don&#039;t think you can append a single byte to a string using the & operator.<BR><BR>You&#039;ll have to try it to find out, I&#039;m afraid.<BR><BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
273

## ya i just get

a dead image link on the page,,but maybe i&#039;m not using the function right..<BR>here&#039;s my code so far:<BR><BR>Set RS = MyConn.Execute("SELECT filedata FROM upload WHERE filenum = 27")<BR><BR>&#039;Set the ContentType to image/gif<BR>Response.ContentType = "image/gif"<BR><BR> Function HexToBinary(hexText)<BR> binary = RS("filedata")<BR> For bytenum = 1 To Len(hexText) Step 2<BR> oneByte = CInt("&H" & Mid(hexText,bytenum,2) )<BR> binary = binary & ChrB(oneByte)<BR> Next<BR>End Function<BR><BR><BR>Response.Write binary<BR>%

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Coded all wrong...

Function HexToBinary(hexText)<BR> binary = ""<BR> For bytenum = 1 To Len(hexText) Step 2<BR> oneByte = CInt("&H" & Mid(hexText,bytenum,2) )<BR> binary = binary & ChrB(oneByte)<BR> Next<BR> HexToBinary = binary<BR>End Function<BR><BR><BR>Response.Write HexToBinary( RS("filedata") )<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
6,476

## RE: ya i just get

6. Senior Member
Join Date
Dec 1969
Posts
273

## RE: Coded all wrong...

now i get this error:<BR>Type mismatch: &#039;CInt&#039;

7. Senior Member
Join Date
Dec 1969
Posts
273

## well, yes and no

I was only able to get it to work if i converted the binary to hex and uploaded it,, but i &#039;m not really sure if that worked,, since now i am having trouble showing the picture from the data base..<BR>if you would still like to help me get this thing working using only binary then that would be very kool!<BR>but we should do it over email instead of adding a million posts to this forum. my email addy : admin at irn dot ca <BR>just type it as u read it so web bots don&#039;t pic up my addy.

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Debug time...

Function HexToBinary(hexText) <BR> binary = "" <BR> For bytenum = 1 To Len(hexText) Step 2 <BR> oneHex = Mid(hexText,bytenum,2)<BR> oneByte = "?"<BR> On Error Resume Next<BR> oneByte = CInt("&H" & oneHex ) <BR> On Error GoTo 0<BR> If oneByte = "?" Then<BR> Response.Write "Failed on &#039;" & oneHex & "&#039;"<BR> Response.End<BR> End If<BR> binary = binary & ChrB(oneByte) <BR> Next <BR> HexToBinary = binary <BR>End Function <BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
6,476

## Maybe Bill can help a bit

as well if needed so do it here real quick.<BR>I will be gone relitively soon and wont be back untill friday sometime.<BR><BR>Im sure once Bill sees it he will understand it.<BR>Again, not sure how long I&#039;ll be here for now, but lets see if we can do it now and get it done with.<BR><BR>Modify this as needed to your requirements.<BR><BR>&#060;%<BR>Set conn = Server.CreateObject("ADODB.Connection") <BR>Set Cmd = Server.CreateObject("ADODB.Command") <BR>Set rs = Server.CreateObject("ADODB.Recordset") <BR>DBOpenString = <BR>"SERVER=mysql.****.com;db=dbname;uid=user;pwd= pass;driver=MySQL; OPTION=35" <BR><BR>conn.open DBOpenString <BR>Cmd.ActiveConnection = conn <BR>rs.Open "TheTableName", conn, 2, 3 <BR><BR><BR>rs.AddNew <BR>rs("PHOTO_ID") = PHOTOID <BR>rs("MEDIA") = objFile.Binary <BR>rs.Update <BR><BR><BR>rs.Close <BR>Set rs = Nothing <BR>Set Cmd = Nothing <BR>Set conn = Nothing <BR>%&#062;<BR><BR>IF you get any errors, post exact error it gives<BR>and the "Exact Code you used" (meaning your modified version)<BR>Then we can take it from there.<BR>

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## If that works w/ MySQL...

...and you say it does, then sure. It&#039;s just that I have *HEARD* others say that they couldn&#039;t get MyODBC driver to handle a blob via the AddNew technique. Again, that&#039;s hearsay, no personal evidence. Maybe you need a specific version of MyODBC??<BR><BR>[Now, please explain the point of the "Cmd" variable in that code?]<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•