Binary to Hex !!!

New to FreeBASIC? Post your questions here.
Alexa
Posts: 56
Joined: May 01, 2007 20:22

Binary to Hex !!!

Postby Alexa » May 01, 2007 20:32

Hi

First Part :
I want to write a program to Open a File and then convert it from Binary to HEX ASCII, for example Open a Bitmap file and then save it as HEX ASCII.

Second Part :
i want to write a program to open a file and search and replace the original bytes with some custom byte, for example i want to replace every "FF" with "FF A0"

i'm also looking for a Compression Lib embedded into my program ( without using DLLs, for example Zlib or ... )

Thank you.
Last edited by Alexa on May 02, 2007 22:29, edited 2 times in total.
arenth
Posts: 511
Joined: Aug 30, 2005 6:22

Postby arenth » May 01, 2007 20:50

Uhh I'm not entirely sure what you mean about the "binary to ascii" thing, since bitmap is binary to begin with.

For the second part, you would simply have to open your file up, search through it, writing a new copy of the file with the bytes you had changed.

The last part you pretty well answered yourself, just take zlib, and make a static library, then link it to your program at the end.
Alexa
Posts: 56
Joined: May 01, 2007 20:22

Postby Alexa » May 01, 2007 20:59

First, Thanks for Replay Arenth

excuse me, i had mistake, i want to convert the file block from Binary to "HEX ASCII" ( ASCII Editing )

and about the Second part, i want to write a program with FB to Search and Replace Bytes, for example replace every "FF" with "FF A0 01"
jevans4949
Posts: 1151
Joined: May 08, 2006 21:58
Location: Crewe, England

Postby jevans4949 » May 02, 2007 0:44

Umm, is this homework?

Open your original file for binary input. Output can be standard text.

Read each byte of source into an UNSIGNED BYTE field. See GET statement. PRINT #outfile, HEX(ubyte,2);

IF ubyte = &HFF THEN PRINT #outfile,"A001";
Alexa
Posts: 56
Joined: May 01, 2007 20:22

Postby Alexa » May 02, 2007 12:46

Thanks Jevans4949

I got the Second Part

but about the first part, i don't understand how ? it just PUT the Binary into output file not HEXASCII of input file . can you help me more ?
Alexa
Posts: 56
Joined: May 01, 2007 20:22

Postby Alexa » May 02, 2007 15:49

Here is my App

Code: Select all

Dim As Integer x
x = FreeFile
Open "1.bmp" For Binary Access Read As #x
Dim As UByte binz( LOF( x ) - 1 )
Get #x, , binz()
Close
x = FreeFile
Open "1.hex" For Binary Access Write As #x
Put #x, , binz()
Close
Print "Done !"
Sleep


i tired to fixing it for ASCII output. :(

=-=-=-=-=-=-=

i also need a Windows GUI Open File Dialog, i want it to Open when my App Start


i very appreciated ... ! =)
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » May 02, 2007 17:43

Alexa, if you mean you want to output the hexadecimal values of the bytes in a file, then you can use the HEX procedure, as jevans4949 suggests. It converts a number to a string representing the hexadecimal value - eg, HEX(255) returns "FF". Just convert each byte in the input file to a hexadecimal string and output those strings to another file.

Perhaps you'll have more luck posting about your open dialog question in the Windows forum. Good luck. :D
Alexa
Posts: 56
Joined: May 01, 2007 20:22

Postby Alexa » May 02, 2007 18:11

I Approve your Suggestion, but i need Source Code :(
Alexa
Posts: 56
Joined: May 01, 2007 20:22

Postby Alexa » May 02, 2007 20:27

Here is the Source for Borland C++

Code: Select all

 AnsiString GetHexString(BYTE *Source,int Length)
{
int i;
    AnsiString tmp;
    AnsiString Result;
    for(i=0; i<Length; i++) {
tmp.sprintf("%02X",Source[i]);
        Result+=tmp;
    }

    return Result;
}


i can do this with Borland C but problem is i have to do this with FreeBasic !!
as you can see by using GetHexString project is done.

or because there is no BinaryToHex command, if there was i'll convert my buffer to Hex by this -> BinaryToHex(Buffer())
MystikShadows
Posts: 606
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Postby MystikShadows » May 03, 2007 1:45

Code: Select all

' Sample Call:  HexResult = BinaryToHex(Buffer())
FUNCTION BinaryToHex(Buffer() AS UBYTE) AS STRING

      DIM Counter    AS INTEGER
      DIM WorkString AS STRING
      DIM WorkValue  AS STRING

      WorkString = ""
      FOR Counter = 1 to UBOUND(Buffer)
          WorkValue = HEX$(Buffer(Counter))
          IF LEN(WorkValue) = 1 THEN
             WorkString = WorkString + "0" + WorkValue + " "
          ELSE
             WorkString = WorkString + WorkValue + " "
          END IF
      NEXT Counter

      BinaryToHex = WorkString

END FUNCTION


after reading a binary file into Buffer() and calling this function you would get a string variable that has something like this:

Code: Select all

3F FF 11 2D 01 00 ...


Depending on what the values of each element of Buffer() held. Of course if your file has a couple 100 Kb of data, it would be a long string ;-)

Hence the sequence would be:

1. read the content of the binary file into Buffer() which is an array of UBYTE
2. DIM HexResult AS STRING
3. HexResult = BinaryToHex(Buffer())
4. Open "Outputfile.txt" FOR OUTPUT AS #5 <- whatever handle you wanna use.
5. PRINT #5, HexResult.
6. CLOSE #5

after running that, you should get a file with all the hex value equivalent of your binary file which can then be edited in a regular text editor.

If you want that split into say 24 hex values per line, you can just use a conter that you increment in the loop and when that counter reaches 24 you Add a CHR$(13) to your string and reset the counter to 1 too be ready for the next line. I can add that in if you want.
Alexa
Posts: 56
Joined: May 01, 2007 20:22

Postby Alexa » May 03, 2007 11:32

Thanks you very much MystikShadows

Let's talk about Reversing it. it may useful for others too.

ASCII -> Binary ! :D
MystikShadows
Posts: 606
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Postby MystikShadows » May 03, 2007 12:26

Reversing a hex string back to a binary array is a loop that converts a few things and assigns them to the proper array index.

here's one way to do it.

Code: Select all


DIM Buffer()  AS BYTE
DIM Counter   AS INTEGER
DIM Position  AS INTEGER
DIM workHex   AS STRING
DIM WorkFile  AS STRING  ' This holds the contents of the text hex file
DIM WorkLeft  AS BYTE    ' Gets the Left hexvalue of the two digits
DIM WorkRight AS BYTE    ' Gets the right hexvalue of the two digits

' Load Text Hex Values as one string into WorkFile

' Then we begin the parsing loop
Position = 1
Counter  = 1
DO
   REDIM PRESERVE Buffer(Counter) AS BYTE
   WorkHex         = MID$(WorkFile, Position, 2)
   WorkLeft        = HexToDecimal(LEFT$(WorkHex, 1))
   WorkRight       = HexToDecimal(RIGHT$(WorkHex, 1))
   Buffer(Counter) = WorkLeft * 16 + WorkRight
   Position        += 3  ' skips the space between the values
   Counter         += 1  ' Increment Counter to get new Buffer Index Value
   IF Position > LEN(WorkFile) THEN EXIT DO
LOOP

FUNCTION HexToDecimal(HexValue AS STRING) AS BYTE

    DIM WorkReturnValue AS BYTE
    DIM WorkLeftValue   AS STRING
    DIM WorkRightValue  AS STRING

    SELECT CASE HexValue
           CASE "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
                WorkReturnValue = VAL(WorkLeftValue)
           CASE "A"
                WorkReturnValue = 10
           CASE "B"
                WorkReturnValue = 11
           CASE "C"
                WorkReturnValue = 12
           CASE "D"
                WorkReturnValue = 13
           CASE "E"
                WorkReturnValue = 14
           CASE "F"
                WorkReturnValue = 15
    END SELECT
    HexToDecimal = WorkReturnValue

END FUNCTION



At this point you have bUffer() which has all the HEX Value equivalents back in the array. You just need to put it back in the file (or do whatever you want with it).
Last edited by MystikShadows on May 03, 2007 14:06, edited 1 time in total.
counting_pine
Site Admin
Posts: 6200
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » May 03, 2007 12:58

Here's a simpler (I hope) version of MystikShadows' code. I know a couple of tricks that make it easier to convert between integers and hex strings.

Converting a buffer into hex:

Code: Select all

' Sample Call:  HexResult = BinaryToHex(Buffer())
Function BinaryToHex(Buffer() As Ubyte) As String
   
    Dim Counter    As Integer
    Dim WorkString As String
    Dim WorkValue  As String
   
    WorkString = ""
    For Counter = LBound(Buffer) To Ubound(Buffer)
        WorkString &= Hex(Buffer(Counter), 2) & " "
    Next Counter
   
    BinaryToHex = WorkString
   
End Function


Converting a hex string into an array:

Code: Select all

Dim Buffer()  As Byte
Dim Counter   As Integer
Dim WorkHex   As String
Dim WorkFile  As String  ' This holds the contents of the text hex file

' Load Text Hex Values as one string into WorkFile

' Then we begin the parsing loop

Redim Preserve Buffer(1 To Len(Workfile) \ 3) As Byte

For Counter = 1 to Len(Workfile) \ 3
   WorkHex         = Mid(WorkFile, Counter * 3 - 2, 2)
   Buffer(Counter) = ValInt("&H" & WorkHex)
Next Counter
Alexa
Posts: 56
Joined: May 01, 2007 20:22

Postby Alexa » May 03, 2007 13:49

Another Problem :(

It works very slow on large files but it works stable, change the 1.mp3 with a ~ 3MB File, and you have to wait for hours i think :((
you should optimize it more

and about counting_pine, your simpler version just work with low size files , and it crash with large files

Code: Select all

Function BinaryToHex(Buffer() As Ubyte) As String

      Dim Counter    As Integer
      Dim WorkString As String
      Dim WorkValue  As String

      WorkString = ""
      For Counter = 1 To Ubound(Buffer)
          WorkValue = Hex$(Buffer(Counter))
          If Len(WorkValue) = 1 Then
             WorkString = WorkString + "0" + WorkValue + ""
          Else
             WorkString = WorkString + WorkValue + ""
          End If
      Next Counter

      BinaryToHex = WorkString

End Function
DIM AS STRING HexResult
Dim As Integer X
Open "1.mp3" For Binary Access Read As #x
Dim As UBYTE Buffer(LOF( x ) - 1)
Get #x, , Buffer()
HexResult = BinaryToHex(Buffer())
Open "1.hex" For Binary Access Write As #5
PRINT #5, HexResult
CLOSE #5
counting_pine
Site Admin
Posts: 6200
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » May 03, 2007 13:58

Which piece of code is crashing? Can you compile with "fbc -exx", run from the command line, and let me know what error you get?

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests