Jump to content

Binary file and string


czmaster
 Share

Recommended Posts

Hi,

If I remove some data (from a binary file) with the String functions (Stringregexp, StringMid, ...) some data are changed by 0x.

If I open the file and show the data without using the String functions the data doesn't change.

For example if I open a GIF file instead of the line GIF89 I have GI0x123456.

Do you know why ?

Thx !

Link to comment
Share on other sites

  • Developers

Hi,

If I remove some data (from a binary file) with the String functions (Stringregexp, StringMid, ...) some data are changed by 0x.

If I open the file and show the data without using the String functions the data doesn't change.

For example if I open a GIF file instead of the line GIF89 I have GI0x123456.

Do you know why ?

Thx !

You are probably doing something wrong but you will have to show the script to be able to see what is happening.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Moderators

czmaster,

Because Autoit variables are not "typed" (that is forced to be something specific like an integer, string, float, etc) you sometimes get the "best guess" of the interpreter as to what a type particular variable is supposed to be. If you open a file in binary mode, Autoit presumes you want the contents as a hex string - and that is what you get: the hex bytes preceded by "0x" to indicate it is hex. Look at this example:

$sFilename = "Your_File_Path"

Global $hFileHandle = FileOpen($sFilename, 16)
$bFileContents = FileRead($hFileHandle)

$bFirstBytes = StringLeft($bFileContents, 16)
$bFirstHex = StringMid($bFileContents, 3, 14)

MsgBox(0, "", $bFirstBytes & @CRLF & "    " & $bFirstHex)

You can see that the "normal" return has the "0x" at the beginning - it is up to you to remove it if you want to manipulate the bytes themselves.

I know it sounds a pain, but it is the price to pay for having un-typed variables - one that I am very happy to pay given the flexibility that it offers. There are a number of commands which force variables into a specifiic type: Dec, Hex, Number, Binary, String, etc - these can be used if you want to change the type. For example, InputBox returns a string - so if you want to compare the result to a number, you need to reset the type first:

$sResult = InputBox(.....
If Number($sResult) > 5 Then
; .....

I hope that answers your question. If not, post your code so we can see what is going on and offer more advice. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

While 1

        $recv = TCPRecv($socket, 8)
        If @error <> 0 Then
            ExitLoop
        EndIf
        If ($x = 1) Then
            $txt &= $recv
        EndIf
        ConsoleWrite($recv)
    WEnd
    $f = FileOpen("123.txt", 1)

    $b = 1
    While $b <> 0
        $a = $b + 1
        $b = StringInStr($txt, @CRLF, 0, 1, $a)
        $t = StringMid($txt, $a, $b - $a)
        FileWrite($f, $t & @CRLF)
    Wend

This is a part of code, the entire program donwload a file from an HTTP server and remove chunk information.

When I show value of $recv it looks good, but in my file it isn't the same value.

Thx,

Edited by czmaster
Link to comment
Share on other sites

The file on the server is a binary or text file? Can you post the first few bytes?

The file is a GIF but it can also be other format. The server return ASCII character but it isn't really text.

Authentic file bytes (copy/paste does not work with all character) :

GIF89a çÿ×é³ûÛüÿöÍå’þþý

Result file bytes :

G0x49463839612000200x00E7FF00EE9D93EDøØ
Edited by czmaster
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...