Jump to content

$test contains a number but won't Hex


 Share

Recommended Posts

I have numbers going into a $test2 variable and I am trying to send it to a 2 byte hex like FF. If I let it run as part of the script it fails and does not return a value. If I manually set the value right before like $test2 = 65 then it works, but even though the value already is supposed to be 65 it does not work.. any idea?

For $a = 1 To StringLen($bitbuffer) Step 8

$base = 128
$chunk = StringMid($bitbuffer, $a, 8)
ConsoleWrite($chunk & @CRLF)
$temp2 = 0
For $b = 1 To 8
If StringMid($chunk, $b, 1) = '1' Then $temp2 += $base
$base /= 2
Next
;~ $temp2 = 65
ConsoleWrite('temp:' & $temp2 & @CRLF)
$binary &= Hex($temp2, 2)
ConsoleWrite('debug:' & Hex($temp2, 2) & @CRLF)
Next

If I uncomment out that line "$temp2 = 65" then every line shows the hex "41" but if I remove it it just shows 00 regardless.

What am I missing?

Link to comment
Share on other sites

Here is what I see when I try and hex a value of 65 that gets generated. This is what Hex( throws out when I remove the parameter and just only have the variable in there:

4050400000000000

What on earth is causing that?

Link to comment
Share on other sites

You'd be better off treating the Hex as a string and using StringLeft/Right/Mid to get what you want from it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I don't know what you mean? I only need to turn numbers like 65 into hex like 41 or 255 to FF etc. I have not had this issue using it before. If I specify $test2 with a value SPECIFICALLY like 65 then it works fine. However since I am doing math it seems to be failing on it even though its the same number ?!

EDIT:

I need this to work already, so I am going to post my whole source code even though its not ready yet. Line 186 should be showing hex like FF AC 41 or what ever but it only ever shows up 00. Even if you remove the ", 2" you see just weird crazy stuff that should not be there. I have tried adding all kinds of debugging code to try and figure it out, consolewrites galore but I cannot figure it out.

;~ 6 possible bytes
;~ FF = 0
;~ FF FF = 110
;~ FF FF FF = 1110
;~ FF FF FF FF = 11110
;~ FF FF FF FF FF = 111110
;~ FF FF FF FF FF FF = 1111110
;~ The end of a multi-byte character won't start with 10 (8 bit original binary) aka won't devide by 128

;~ RawEncode('AקCDE×T]s')
$timer = TimerInit()
;~ $file = FileOpen(@DesktopDir & 'test4.txt')
;~ $file = FileOpen(@DesktopDir & 'exe.exe')


;~ $contents = FileRead($file)
;~ MemGetStats
;~ ConsoleWrite(AscW(StringMid($contents, 3, 1)))
;~ $test = StringMid($contents, 1, 10)

;~ For $a = 1 To StringLen($test)
;~ ConsoleWrite('Character ' & $a & ':' & AscW(StringMid($test, $a, 1)) & @CRLF)
;~ Next
;~ Exit



;~ ClipPut($test)
;~ ConsoleWrite($test & @CRLF)
;~ $result = Encode('•')
;~ Exit

;~ $result = Encode('XXXXX')
;~ $result = Encode($contents)

;~ ConsoleWrite(@CRLF & @CRLF & $result & @CRLF & ' Time taken: ' & TimerDiff($timer) / 1000 & @CRLF)
;~ ClipPut($result)


Func Encode($input, $file = 0)
Local $list[65], $result, $group[4], $b64[5], $bitbuffer, $binary, $place
For $a = 0 To 63
Select
Case $a < 26
$list[$a] = ChrW(65 + $a)
Case $a < 52
$list[$a] = ChrW(71 + $a)
Case $a < 62
$list[$a] = ChrW($a - 4)
Case $a = 62
$list[$a] = ChrW(43)
Case $a = 63
$list[$a] = ChrW(47)
EndSelect
Next
$input = StringTrimLeft(StringToBinary($input, 1), 2)
For $a = 1 To StringLen($input) Step 6
$3byte = StringMid($input, $a, 6)
$pos = 0
$charn = 0
For $b = 1 To 6 Step 2 ; This generates an array of 3 bytes represented in numbers
$pos += 1
If StringMid($3byte, $b, 2) = '' And $pos > 1 Then $charn += 1
$group[$pos] = Number('0x' & StringMid($3byte, $b, 2))
Next
$binary = ''
For $c = 1 To 3 ; This processes groups of 3 bytes in their binary number form via an array
$bit = 128
While $bit >= 1
If Floor($group[$c] / $bit) >= 1 Then
$binary &= 1
$group[$c] -= $bit
Else
$binary &= 0
EndIf
$bit /= 2
WEnd
If $c = 3 Then ; Here we now have a 24 bit binary rep of 3 bytes in $binary
$encnum = 0
For $d = 1 To 24 Step 6 ; This splits the 24 bit binary into 6 bit binary groupings
$6bit = StringMid($binary, $d, 6)
$64value = 0
$bit = 32
For $process6bit = 1 To 6
If StringMid($6bit, $process6bit, 1) = 1 Then
$64value += $bit
EndIf
$bit /= 2
Next
$encnum += 1
If $encnum <= (4 - $charn) Then
$result &= $list[$64value]
Else
$result &= '='
EndIf
If $encnum = 4 Then $encnum = 0
$64value = 0
Next
EndIf
Next
Next
$final = ''
For $a = 1 To StringLen($result) Step 64
$final &= StringMid($result, $a, 64) & @CRLF
Next


Return $final
EndFunc ;==>Encode

;~ 8KStoms=
;~ X WA==
;~ XX WFg=
;~ XXX WFhY
;~ XXXX WFhYWA==
;~ XXXXX WFhYWFg=


$encoded = Encode('AB0123')
ConsoleWrite('Encoded:' & $encoded & @CRLF)

ConsoleWrite('Decoded:' & Decode($encoded) & @CRLF)


Func Decode($input = '', $file = 0)
Local $list[65], $result, $group[4], $b64[5], $bitbuffer, $binary, $temp, $prehex
For $a = 0 To 63
Select
Case $a < 26
$list[$a] = ChrW(65 + $a)
Case $a < 52
$list[$a] = ChrW(71 + $a)
Case $a < 62
$list[$a] = ChrW($a - 4)
Case $a = 62
$list[$a] = ChrW(43)
Case $a = 63
$list[$a] = ChrW(47)
EndSelect
Next

For $a = 1 To StringLen($input) Step +1

For $b = 1 To 63
If StringInStr($list[$b], StringMid($input, $a, 1), 1) Then

$base = 32
$temp = $b

While $base >= 1
If Floor($temp / $base) Then
$bitbuffer &= '1'
$temp -= (Floor($temp / $base) * $base)
Else
$bitbuffer &= '0'
EndIf
$base /= 2
WEnd
EndIf
Next
Next

Global $prehex, $place
$binary = '0x'
;~ ConsoleWrite('Bit buffer:' & $bitbuffer & @CRLF)
For $a = 1 To StringLen($bitbuffer) Step 8
$prehex = 0
$place = 128
;~ ConsoleWrite(Hex($prehex + $place) & @CRLF)
$chunk = StringMid($bitbuffer, $a, 8)
;~ ConsoleWrite($chunk & @CRLF)

For $b = 1 To 8
If StringMid($chunk, $b, 1) = '1' Then

$prehex += $place

Else
$prehex += 0

EndIf
;~ ConsoleWrite($prehex & @CRLF)
$place /= 2
Next
ConsoleWrite(Hex($prehex, 2) & @CRLF)
;~ ConsoleWrite(Hex($prehex, 2) & @CRLF)
;~ $ggg = Hex($prehex, 2)
;~ ConsoleWrite($ggg & @CRLF)
;~ ConsoleWrite('debug:' & Hex($prehex, 2) & @CRLF)
;~ $prehex = 0
;~ ConsoleWrite('temp:' & $prehex & @CRLF)
;~ $binary &= Hex($prehex, 2)

;~ ConsoleWrite('StringisDIGIT:' & StringIsDigit($prehex) & @CRLF)



Next

Return $binary

EndFunc ;==>Decode


Func hexi($input = 0)
$blah = Hex($input, 2)
return $blah

EndFunc
Edited by Morthawt
Link to comment
Share on other sites

Do you know what's the difference between integer and double-precision number?

Local $iInteger = 10
Local $fDouble = 2 * $iInteger / 2

ConsoleWrite("Integer, value = " & $iInteger & ", Hex representation = " & Hex($iInteger) & @CRLF)
ConsoleWrite("Double, value = " & $fDouble & ", Hex representation = " & Hex($fDouble) & @CRLF)

You are seeing just that. Both numbers are 10, but former is integer and latter is not. Your mathematical operation changed the type of the variable. If you want your number to be integer then you have to Int() it. Hex() shows binary representation of the number which is exactly how it's stored in memory of your computer.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Do you know what's the difference between integer and double-precision number?

Local $iInteger = 10
Local $fDouble = 2 * $iInteger / 2

ConsoleWrite("Integer, value = " & $iInteger & ", Hex representation = " & Hex($iInteger) & @CRLF)
ConsoleWrite("Double, value = " & $fDouble & ", Hex representation = " & Hex($fDouble) & @CRLF)

You are seeing just that. Both numbers are 10, but former is integer and latter is not. Your mathematical operation changed the type of the variable. If you want your number to be integer then you have to Int() it. Hex() shows binary representation of the number which is exactly how it's stored in memory of your computer.

ah thanks
Link to comment
Share on other sites

Use varGetType to check the type of a variable:

Local $iInteger = 10
Local $fDouble = 2 * $iInteger / 2

ConsoleWrite("Integer, value = " & $iInteger & ", Hex representation = " & Hex($iInteger) & @CRLF)
ConsoleWrite("Double, value = " & $fDouble & ", Hex representation = " & Hex($fDouble) & @CRLF)

ConsoleWrite("VarGetType($iInteger): " & VarGetType($iInteger) & @LF)
ConsoleWrite("VarGetType($fDouble): " & VarGetType($fDouble) & @LF)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks, that was driving me crazy. Before I had one line setting the initial value, one line making the change needed and one line dividing the first initial value by 2 so it went smoothly down. After 3 hours of being totally stumped I ended up having a switch that has 8 cases. Is there any benefit with this double-precision? I mean a number is a number, I checked it a million different ways, same string length, is a digit, is a number, it would add and subtract to different values etc. So what is the purpose behind a double-precision ?

Link to comment
Share on other sites

Wikipedia tells you.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thanks, well I dealt with that issue so the only one remaining is this. I am trying to convert typed text or files to base64 and am having some issues with unicode vs ascii or what ever. If I leave the stringtobinary at 1 parameter than I can encode and decode an exe file perfectly. However when I then convert a file containing chinese letters or what ever it messes up.

When I read the file as UTF-8 and encode the file the exe looks wrong right from the start. Even online converters where I have converted exe's always have the encoded part at the start as "TVqQ" and when I encode the exe after swapping to binary UTF mode is "TVrC". So my script is not doing something correctly or something. I figured if I use unicode for everything to do with my conversion that it would just be universal and work with everything. Any chance you could have a quick look at my script and see if anything jumps out at you? For your own testing pick any old exe and put it on the desktop and alter the file name of it at the top of the script so it loads your exe because I am using a 13kb file from www.grc.com because this program is very slow for encoding big files.

#NoTrayIcon
#include 
Global $timer


$use_UTF_8 = 1

;~ $result = Encode('AB0123')
;~ $result = Encode('XXXX')

;~ $open = FileOpen(@DesktopDir & 'Test2.txt')
$open = FileOpen(@DesktopDir & 'id.exe')
$file = FileRead($open)

;~ ConsoleWrite('Encoded:' & $encoded & @CRLF)

;~ ConsoleWrite('Decoded:' & Decode($encoded) & @CRLF)
;~ ConsoleWrite(Encode(StringMid($file, 1, 10)) & @CRLF & $timer)
;~ $result = encode(StringMid($file, 1, 30))
$result = encode($file)
ClipPut($result)
ConsoleWrite($result & @CRLF & $timer)
;~ 8KStoms=
;~ X WA==
;~ XX WFg=
;~ XXX WFhY
;~ XXXX WFhYWA==
;~ XXXXX WFhYWFg=


Func Encode($input, $file = 0)
$timer = TimerInit()
Local $list[65], $result, $group[4], $b64[5], $bitbuffer, $binary, $place
;~ ProgressOn('Encoding', 'Conversion initiated', 'Converting data to base64...')
For $a = 0 To 63
Select
Case $a < 26
$list[$a] = ChrW(65 + $a)
Case $a < 52
$list[$a] = ChrW(71 + $a)
Case $a < 62
$list[$a] = ChrW($a - 4)
Case $a = 62
$list[$a] = ChrW(43)
Case $a = 63
$list[$a] = ChrW(47)
EndSelect
Next
;~ $input = StringToBinary($input, 1)
If $use_UTF_8 = 1 Then
$use_UTF_8 = 4
Else
$use_UTF_8 = 1
EndIf
$input = StringTrimLeft(StringToBinary($input, $use_UTF_8), 2)
ConsoleWrite('Original Hex:' & $input & @CRLF & @CRLF)

For $a = 1 To StringLen($input) Step 6
;~ ProgressSet(($a / StringLen($input)) * 100)
$3byte = StringMid($input, $a, 6)
$pos = 0
$charn = 0
For $b = 1 To 6 Step 2 ; This generates an array of 3 bytes represented in numbers
$pos += 1
If StringMid($3byte, $b, 2) = '' And $pos > 1 Then $charn += 1
$group[$pos] = Number('0x' & StringMid($3byte, $b, 2))
Next
$binary = ''
For $c = 1 To 3 ; This processes groups of 3 bytes in their binary number form via an array
$bit = 128
While $bit >= 1
If Floor($group[$c] / $bit) >= 1 Then
$binary &= 1
$group[$c] -= $bit
Else
$binary &= 0
EndIf
$bit /= 2
WEnd
If $c = 3 Then ; Here we now have a 24 bit binary rep of 3 bytes in $binary
$encnum = 0
For $d = 1 To 24 Step 6 ; This splits the 24 bit binary into 6 bit binary groupings
$6bit = StringMid($binary, $d, 6)
$64value = 0
$bit = 32
For $process6bit = 1 To 6
If StringMid($6bit, $process6bit, 1) = 1 Then
$64value += $bit
EndIf
$bit /= 2
Next
$encnum += 1
If $encnum <= (4 - $charn) Then
$result &= $list[$64value]
Else
$result &= '='
EndIf
If $encnum = 4 Then $encnum = 0
$64value = 0
Next
EndIf
Next
Next
$final = ''
$length = Floor(StringLen($result) / 64) * 64
For $a = 1 To StringLen($result) Step 64
$final &= StringMid($result, $a, 64)
If $a < $length Then $final &= @CRLF
Next
;~ ProgressOff()
$timer = Round(TimerDiff($timer) / 1000, 3)
Return $final
EndFunc ;==>Encode


Func Decode($input = '', $file = 0)
$timer = TimerInit()
Local $list[65], $result, $group[4], $b64[5], $bitbuffer, $binary, $temp, $prehex
$input = StringReplace($input, @CR, '')
$input = StringReplace($input, @LF, '')
;~ ConsoleWrite('Decode:' & $input & @CRLF)
;~ ProgressOn('Decoding', 'Conversion initiated', 'Converting base64 data to it''s original format...')
For $a = 0 To 63
Select
Case $a < 26
$list[$a] = ChrW(65 + $a)
Case $a < 52
$list[$a] = ChrW(71 + $a)
Case $a < 62
$list[$a] = ChrW($a - 4)
Case $a = 62
$list[$a] = ChrW(43)
Case $a = 63
$list[$a] = ChrW(47)
EndSelect
Next
For $a = 1 To StringLen($input) Step +1
;~ ProgressSet(($a / StringLen($input)) * 100)
For $b = 1 To 63
If StringInStr($list[$b], StringMid($input, $a, 1), 1) Then
$base = 32
$temp = $b
While $base >= 1
If Floor($temp / $base) Then
$bitbuffer &= '1'
$temp -= (Floor($temp / $base) * $base)
Else
$bitbuffer &= '0'
EndIf
$base /= 2
WEnd
EndIf
Next
Next
Global $prehex, $place
$binary = '0x'
For $a = 1 To StringLen($bitbuffer) Step 8
$prehex = 0
$chunk = StringMid($bitbuffer, $a, 8)
For $b = 1 To 8
If StringMid($chunk, $b, 1) = '1' Then
Switch $b
Case 1
$prehex += 128
Case 2
$prehex += 64
Case 3
$prehex += 32
Case 4
$prehex += 16
Case 5
$prehex += 8
Case 6
$prehex += 4
Case 7
$prehex += 2
Case 8
$prehex += 1
EndSwitch
EndIf
Next
$binary &= Hex($prehex, 2)
Next
;~ ProgressOff()
$timer = Round(TimerDiff($timer) / 1000, 3)
Return BinaryToString($binary, $use_UTF_8)
EndFunc ;==>Decode






Exit
#include 
#include 
#include 
#include 
#include 
#region ### START Koda GUI section ### Form=
$GUI = GUICreate("Base64 Conversions", 615, 315, -1, -1)
$text = GUICtrlCreateEdit("", 16, 40, 577, 137, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL, $WS_BORDER), $WS_EX_STATICEDGE)
GUICtrlSetData(-1, "")

$Label1 = GUICtrlCreateLabel("Content to convert:", 16, 8, 225, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)

$plain2 = GUICtrlCreateButton("Plain > Base64", 176, 185, 107, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$base2 = GUICtrlCreateButton("Base64 > Plain", 296, 185, 107, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)

$Label9 = GUICtrlCreateLabel("Convert Files", 236, 240, 150, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)

$convertp = GUICtrlCreateButton("Plain file > B64", 168, 270, 115, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)

$convertb = GUICtrlCreateButton("B64 file > Plain", 304, 270, 115, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)

GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit

Case $plain2
GUICtrlSetData($text, Encode(GUICtrlRead($text)))

Case $base2
GUICtrlSetData($text, Decode(GUICtrlRead($text)))

Case $convertp
$filelocation = FileOpenDialog('Choose file', @UserProfileDir, 'All Files (*.*)', 1)
If $filelocation <> '' Then
Local $szDrive, $szDir, $szFName, $szExt, $encoded, $result = ''
_PathSplit($filelocation, $szDrive, $szDir, $szFName, $szExt)
$savelocation = FileSaveDialog('Save as', @UserProfileDir, 'All Files (*.*)', 16, $szFName & '(Encoded)' & $szExt)
If @error Then ContinueCase
$read = FileRead($filelocation)
$encoded = Encode($read)
$length = Floor(StringLen($encoded) / 64) * 64
For $a = 1 To StringLen($encoded) Step +64
$result &= StringMid($encoded, $a, 64)
Next
FileDelete($savelocation)
$savefile = FileOpen($savelocation, 18)
FileWrite($savefile, $result)
MsgBox(0, 0, $timer)
EndIf

Case $convertb
$filelocation = FileOpenDialog('Choose file', @UserProfileDir, 'All Files (*.*)', 1)
If $filelocation <> '' Then
Local $szDrive, $szDir, $szFName, $szExt
_PathSplit($filelocation, $szDrive, $szDir, $szFName, $szExt)
$savelocation = FileSaveDialog('Save as', @UserProfileDir, 'All Files (*.*)', 16, StringReplace($szFName, '(Encoded)', '', -1) & '(Decoded)' & $szExt)
If @error Then ContinueCase
$read = FileRead($filelocation)
$result = $read
$result = String(StringReplace($result, @CR, ''))
$result = StringReplace($result, @LF, '')
$result = Decode($result)
FileDelete($savelocation)
FileWrite($savelocation, $result)
MsgBox(0, 0, $timer)
EndIf

EndSwitch
WEnd

Oh and by the way, I included an easy to locate variable near the top which you can 1 or 0 to go back and forth from the normal stringtobinary vs the UTF one.

Link to comment
Share on other sites

Ok, I rewrote the entire thing and it is a lot cleaner code and I think it works better. My issue is with decoding a previously encoded UTF-8 file.. I can type UTF things into the text area on the GUI and it decodes fine, however when I try and decode a correctly encoded file it ends up missing those UTF characters and instead only seeing "?".

Could someone have a quick look through my code and see if anything jumps out at you? It has to be something to do with the writing to file part because as I said it works fine for text in the box, decode it and boom... you see all the wacky UTF symbols etc perfectly in there.

#include 
#include 
#include 
#include 
#include 
#include 
#include 
Global $list[64]
init()

Func Decode($input = '')
Local $TheBitStream, $4_SixBitChunks, $FinalOutput, $tempvalue
$input = StringReplace($input, '=', '')
$input = StringReplace($input, @CR, '')
$input = StringReplace($input, @LF, '')
For $a = 1 To StringLen($input)
$pos = _ArraySearch($list, StringMid($input, $a, 1), 0, 0, 1)
$TheBitStream &= SixBitBinary($pos)
Next
For $a = 1 To StringLen($TheBitStream) Step 8
$tempvalue &= Hex(String(FromEightBitBinary(StringMid($TheBitStream, $a, 8))), 2)
Next
Return BinaryToString('0x' & $tempvalue, 4)
EndFunc ;==>Decode

Func Encode($input = '')
Local $TheBitStream, $4_SixBitChunks, $FinalOutput, $tempvalue
$o_UTF_8 = StringTrimLeft(StringToBinary($input, 4), 2)
For $a = 1 To StringLen($o_UTF_8) Step 2
$TheBitStream &= EightBitBinary('0x' & StringMid($o_UTF_8, $a, 2))
Next
For $a = 1 To StringLen($TheBitStream) Step +6
$Number = FromSixBitBinary(StringMid($TheBitStream, $a, 6))
$FinalOutput &= $list[$Number]
Next
While Floor(StringLen($FinalOutput) / 4) <> (StringLen($FinalOutput) / 4)
$FinalOutput &= '='
WEnd
Return $FinalOutput
EndFunc ;==>Encode

Func EightBitBinary($input = 0)
If $input < 256 Then
$tmpbitstream = ''
$start = 128
While $start >= 1
If Floor($input / $start) Then
$tmpbitstream &= 1
$input -= ($start * Floor($input / $start))
Else
$tmpbitstream &= 0
EndIf
$start /= 2
WEnd
Else
$tmpbitstream = 0
EndIf
Return $tmpbitstream
EndFunc ;==>EightBitBinary

Func SixBitBinary($input = 0)
If $input < 64 Then
$tmpbitstream = ''
$start = 32
While $start >= 1
If Floor($input / $start) Then
$tmpbitstream &= 1
$input -= ($start * Floor($input / $start))
Else
$tmpbitstream &= 0
EndIf
$start /= 2
WEnd
Else
$tmpbitstream = 0
EndIf
Return $tmpbitstream
EndFunc ;==>SixBitBinary

Func FromSixBitBinary($input = 0)
$base = 32
$tempvalue = 0
For $a = 1 To 6
If StringMid($input, $a, 1) = 1 Then
$tempvalue += $base
EndIf
$base /= 2
Next
Return $tempvalue
EndFunc ;==>FromSixBitBinary

Func FromEightBitBinary($input = 0)
$base = 128
$tempvalue = 0
For $a = 1 To 8
If StringMid($input, $a, 1) = 1 Then
$tempvalue += $base
EndIf
$base /= 2
Next
Return $tempvalue
EndFunc ;==>FromEightBitBinary

Func init()
For $a = 0 To 63
Select
Case $a < 26
$list[$a] = ChrW(65 + $a)
Case $a < 52
$list[$a] = ChrW(71 + $a)
Case $a < 62
$list[$a] = ChrW($a - 4)
Case $a = 62
$list[$a] = ChrW(43)
Case $a = 63
$list[$a] = ChrW(47)
EndSelect
Next
EndFunc ;==>init

#region ### START Koda GUI section ### Form=
$GUI = GUICreate("Base64 Conversions", 615, 315, -1, -1)
$text = GUICtrlCreateEdit("", 16, 40, 577, 137, BitOR($ES_AUTOVSCROLL, $ES_WANTRETURN, $WS_VSCROLL, $WS_BORDER), $WS_EX_STATICEDGE)
GUICtrlSetData(-1, "")
$Label1 = GUICtrlCreateLabel("Content to convert:", 16, 8, 225, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$plain2 = GUICtrlCreateButton("Plain > Base64", 176, 185, 107, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$base2 = GUICtrlCreateButton("Base64 > Plain", 296, 185, 107, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$Label9 = GUICtrlCreateLabel("Convert Files", 236, 240, 150, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$convertp = GUICtrlCreateButton("Plain file > B64", 168, 270, 115, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$convertb = GUICtrlCreateButton("B64 file > Plain", 304, 270, 115, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $plain2
GUICtrlSetData($text, Encode(GUICtrlRead($text)))
Case $base2
GUICtrlSetData($text, Decode(GUICtrlRead($text)))
Case $convertp
$filelocation = FileOpenDialog('Choose file', @UserProfileDir, 'All Files (*.*)', 1)
If $filelocation <> '' Then
Local $szDrive, $szDir, $szFName, $szExt, $encoded, $result = ''
_PathSplit($filelocation, $szDrive, $szDir, $szFName, $szExt)
$savelocation = FileSaveDialog('Save as', @UserProfileDir, 'All Files (*.*)', 16, $szFName & '(Encoded)' & $szExt)
If @error Then ContinueCase
$read = FileRead($filelocation)
$encoded = Encode($read)
For $a = 1 To StringLen($encoded) Step +64
$result &= StringMid($encoded, $a, 64) & @CRLF
Next
$savefile = FileOpen($savelocation, 18)
FileWrite($savefile, $result)
FileClose($savefile)
MsgBox(0, 'Completes', 'Process has completed')
EndIf
Case $convertb
$filelocation = FileOpenDialog('Choose file', @UserProfileDir, 'All Files (*.*)', 1)
If $filelocation <> '' Then
Local $szDrive, $szDir, $szFName, $szExt
_PathSplit($filelocation, $szDrive, $szDir, $szFName, $szExt)
$savelocation = FileSaveDialog('Save as', @UserProfileDir, 'All Files (*.*)', 16, StringReplace($szFName, '(Encoded)', '', -1) & '(Decoded)' & $szExt)
If @error Then ContinueCase
$read = FileRead($filelocation)
$result = $read
$result = String(StringReplace($result, @CR, ''))
$result = StringReplace($result, @LF, '')
$result = Decode($result)
$savefile = FileOpen($savelocation, 18)
FileWrite($savefile, $result)
FileClose($savefile)
MsgBox(0, 'Completes', 'Process has completed')
EndIf
EndSwitch
WEnd
Link to comment
Share on other sites

I got it working. I have been bounding ideas around with a friend. It turned out I needed to treat file encoding differently because there is raw hex bits (binary) to work with. The typing into the box part I have all in UTF-8 now and it works flawlessly. I will paste it here. It is a lot better code than any of the other 4 versions I have coded. I would welcome some feedback on the methodology I have used etc.

#include 
#include 
#include 
#include 
#include 
#include 
#include 
Global $list[64], $timer
#NoTrayIcon

init()
Func Decode($input = '', $isfile = 0)
$timer = TimerInit()
Local $TheBitStream, $4_SixBitChunks, $FinalOutput, $tempvalue
If $isfile Then
$openfile = FileOpen($input, 0)
$input = FileRead($openfile)
FileClose($openfile)
EndIf
$input = StringReplace($input, '=', '')
$input = StringReplace($input, @CR, '')
$input = StringReplace($input, @LF, '')
For $a = 1 To StringLen($input)
$case = 'N'
If StringIsUpper(StringMid($input, $a, 1)) Then $case = 'U'
$TheBitStream &= SixBitBinary(Eval($case & StringMid($input, $a, 1)))
Next
For $a = 1 To StringLen($TheBitStream) Step 8
$tempvalue &= Hex(String(FromEightBitBinary(StringMid($TheBitStream, $a, 8))), 2)
Next
If Not $isfile Then
$FinalOutput = BinaryToString('0x' & $tempvalue, 4)
Else
$FinalOutput = '0x' & $tempvalue
EndIf
Return $FinalOutput
EndFunc ;==>Decode
Func Encode($input = '', $isfile = 0)
$timer = TimerInit()
Local $TheBitStream, $4_SixBitChunks, $FinalOutput, $tempvalue, $FinalOutput2
If Not $isfile Then
$o_UTF_8 = StringTrimLeft(StringToBinary($input, 4), 2)
Else
$openfile = FileOpen($input, 16)
$o_UTF_8 = StringTrimLeft(FileRead($openfile), 2)
FileClose($openfile)
EndIf
For $a = 1 To StringLen($o_UTF_8) Step 2
$TheBitStream &= EightBitBinary('0x' & StringMid($o_UTF_8, $a, 2))
Next
For $a = 1 To StringLen($TheBitStream) Step +6
$Number = FromSixBitBinary(StringMid($TheBitStream, $a, 6))
$FinalOutput &= $list[$Number]
Next
While Floor(StringLen($FinalOutput) / 4) <> (StringLen($FinalOutput) / 4)
$FinalOutput &= '='
WEnd
For $a = 1 To StringLen($FinalOutput) Step +64
$FinalOutput2 &= StringMid($FinalOutput, $a, 64)
If $a < (Floor(StringLen($FinalOutput) / 64) * 64) Then $FinalOutput2 &= @CRLF
Next
Return $FinalOutput2
EndFunc ;==>Encode
Func EightBitBinary($input = 0)
If $input < 256 Then
$tmpbitstream = ''
$start = 128
While $start >= 1
If Floor($input / $start) Then
$tmpbitstream &= 1
$input -= ($start * Floor($input / $start))
Else
$tmpbitstream &= 0
EndIf
$start /= 2
WEnd
Else
$tmpbitstream = 0
EndIf
Return $tmpbitstream
EndFunc ;==>EightBitBinary
Func SixBitBinary($input = 0)
If $input < 64 Then
$tmpbitstream = ''
$start = 32
While $start >= 1
If Floor($input / $start) Then
$tmpbitstream &= 1
$input -= ($start * Floor($input / $start))
Else
$tmpbitstream &= 0
EndIf
$start /= 2
WEnd
Else
$tmpbitstream = 0
EndIf
Return $tmpbitstream
EndFunc ;==>SixBitBinary
Func FromSixBitBinary($input = 0)
$base = 32
$tempvalue = 0
For $a = 1 To 6
If StringMid($input, $a, 1) = 1 Then
$tempvalue += $base
EndIf
$base /= 2
Next
Return $tempvalue
EndFunc ;==>FromSixBitBinary
Func FromEightBitBinary($input = 0)
$base = 128
$tempvalue = 0
For $a = 1 To 8
If StringMid($input, $a, 1) = 1 Then
$tempvalue += $base
EndIf
$base /= 2
Next
Return $tempvalue
EndFunc ;==>FromEightBitBinary
Func init()
For $a = 0 To 63
Select
Case $a < 26
$list[$a] = ChrW(65 + $a)
Assign('U' & ChrW(65 + $a), $a, 2)
Case $a < 52
$list[$a] = ChrW(71 + $a)
Assign('N' & ChrW(71 + $a), $a, 2)
Case $a < 62
$list[$a] = ChrW($a - 4)
Assign('N' & ChrW($a - 4), $a, 2)
Case $a = 62
$list[$a] = ChrW(43)
Assign('N' & ChrW(43), $a, 2)
Case $a = 63
$list[$a] = ChrW(47)
Assign('N' & ChrW(47), $a, 2)
EndSelect
Next
EndFunc ;==>init
$GUI = GUICreate("Base64 Conversions", 615, 315, -1, -1)
$text = GUICtrlCreateEdit("", 16, 40, 577, 137, BitOR($ES_AUTOVSCROLL,$ES_WANTRETURN,$WS_VSCROLL,$WS_BORDER))
GUICtrlSetFont(-1, 11, 400, 0, "Lucida Console")
GUICtrlSetColor(-1, 0x000000)
$Label1 = GUICtrlCreateLabel("Content to convert:", 16, 8, 225, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$plain2 = GUICtrlCreateButton("Plain > Base64", 176, 185, 107, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$base2 = GUICtrlCreateButton("Base64 > Plain", 296, 185, 107, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$Label9 = GUICtrlCreateLabel("Convert Files", 236, 240, 150, 28)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$convertp = GUICtrlCreateButton("Plain file > B64", 168, 270, 115, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
$convertb = GUICtrlCreateButton("B64 file > Plain", 304, 270, 115, 25)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000000)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $plain2
GUICtrlSetData($text, Encode(GUICtrlRead($text)))
Case $base2
GUICtrlSetData($text, Decode(GUICtrlRead($text)))
Case $convertp
$filelocation = FileOpenDialog('Choose file', @UserProfileDir, 'All Files (*.*)', 1)
If $filelocation <> '' Then
Local $szDrive, $szDir, $szFName, $szExt, $encoded, $result = ''
_PathSplit($filelocation, $szDrive, $szDir, $szFName, $szExt)
$savelocation = FileSaveDialog('Save as', @UserProfileDir, 'All Files (*.*)', 16, $szFName & '(Encoded)' & $szExt)
If @error Then ContinueCase
$encoded = Encode($filelocation, 1)
$savefile = FileOpen($savelocation, 18)
FileWrite($savefile, $encoded)
FileClose($savefile)
MsgBox(0, Round(TimerDiff($timer) / 1000, 3) & ' Seconds', 'Process has completed')
EndIf
Case $convertb
$filelocation = FileOpenDialog('Choose file', @UserProfileDir, 'All Files (*.*)', 1)
If $filelocation <> '' Then
Local $szDrive, $szDir, $szFName, $szExt
_PathSplit($filelocation, $szDrive, $szDir, $szFName, $szExt)
$savelocation = FileSaveDialog('Save as', @UserProfileDir, 'All Files (*.*)', 16, StringReplace($szFName, '(Encoded)', '', -1) & '(Decoded)' & $szExt)
If @error Then ContinueCase
$encoded = Decode($filelocation, 1)
$savefile = FileOpen($savelocation, 18)
FileWrite($savefile, $encoded)
FileClose($savefile)
MsgBox(0, Round(TimerDiff($timer) / 1000, 3) & ' Seconds', 'Process has completed')
EndIf
EndSwitch
WEnd
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...