Jump to content

String to Byte


Chika95
 Share

Recommended Posts

I am trying to take a text file that looks like this:

254 135 166 59 6

Where each number is between 0 and 255. Convert that number to the binary equivalant, and store it directly to a file.

For example. Given the "text" file above, create a file that if opened in a hex editor would yeild:

FE 87 A6 3B 06

which of course are the hexidecimal equivalants of each of the byte above

I've written a rudimentary script to parse the lines based a delimiter (in this case " "), but I'm having trouble trying to get it to save in "byte mode". Feel free to use the attached text file of which is just the first string text above

;String to Hex converter
#include <String.au3>
#include <File.au3>
$FilePath = FileOpenDialog ( "Open File", @ScriptDir,"")
$Delimiter = Inputbox("typed Delimiter","Delimiter:"," ")

$OpenFile = FileOpen ( $FilePath,0)


    $i = 1
    $j = 1
    
    $NewFile = FileOpen(@ScriptDir & "\Hex.txt",26)
    if $NewFile = -1 then
        msgbox(0,"File Open Failed","")
        Exit
    EndIf
While 1
    $CurrentLine = FileReadLine ( $OpenFile,$i )
    ;msgbox(0,"",$CurrentLine)
    if @error = -1 then ExitLoop ;Finished with File
    $StringArray = StringSplit ( $CurrentLine, $Delimiter)
    for $j = 1 to $StringArray[0]
        $Hexed = binary(hex($StringArray[$j],2))
        if msgbox(1,$StringArray[$j],$Hexed) = 2 then
            ShellExecute(@ScriptDir & "\Hex.txt")
            Exit
        EndIf
        If IsBinary ( $Hexed ) = 0 then 
            msgbox(0,"Not Binary","")
            Exit
        EndIf
        IF FileWrite($NewFile, $Hexed)= 0 then 
            if msgbox(0,"File Write Failed","") then exit
        endif
    next
    
    $i = $i+1
Wend

What am I doing wrong?

string_file.txt

Link to comment
Share on other sites

Here's a small demonstration of one way to do what you want.

$Delimiter = " ";

$OpenFile = FileOpen("bintest2.hex", 17);binary mode + write mode
$CurrentLine = "254 135 166 59 6"
$StringArray = StringSplit($CurrentLine, $Delimiter)
$hexed = ''
For $j = 1 To $StringArray[0]
    $hexed &= Chr($StringArray[$j])
Next
FileWrite($OpenFile, $hexed)
FileClose($OpenFile)
Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Alright, so I got that, and I'm able to write any of the 256 Bytes to a file by using ASCII, however Autoit doesn't seem to like to write the first byte associated with the ASCII character "00", for example

#include <String.au3>
$byte = 00 ;Enter write byte to write 0-255
$Text = Chr(Asc(_HexToString($byte))) ;converts the number byte into an ASCII character to write to a file so that the $byte is written as the value when viewed in a hex editor
$FileHandle = FileOpen(@scriptdir & "\Test.txt",10) ;Opens/creates a Test.txt file
FileWrite($FileHandle, $Text) ;Writes the byte, overwriting the file
ShellExecute(@scriptdir & "\Test.txt") ;Opens the file for quick viewing

This creates a file that contains one byte, the byte: 0000 0001. You can change the first line to any byte from 0-255, and it works...except for byte = 0 (0000 0000). When you choose byte 0, it writes nothing, as in a blank file.

At the end I'm trying to write a basic encryption program, where I'm reading in blocks of data from a file, shuffling them around using a custom algorythm, and saving them to a new file (an "encrypted" file). But I can not write the Byte 0000 0000. If there is an easier way to write a byte to a file using autoit then what I've written above, that advice would also be welcomed.

Link to comment
Share on other sites

Figured it out, and a much better way:

#include <String.au3>
$byte = 1 ;Enter write byte to write 0-255
$Text = binary("0x" & Hex($byte,2)) ;converts the number byte into an ASCII character to write to a file so that the $byte is written as the value when viewed in a hex editor
If msgbox(1,isbinary($Text), $Text) = 2 then exit
$FileHandle = FileOpen(@scriptdir & "\Test.txt",26) ;Opens/creates a Test.txt file
FileWrite($FileHandle, $Text) ;Writes the byte, overwriting the file
ShellExecute(@scriptdir & "\Test.txt") ;Opens the file for quick viewing
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...