# How to get arbitrary section of bits from a binary

## Recommended Posts

I want to take a binary data from any source (string,number,files,etc..) and iterate over each X bits of it in a loop, say take bits 1-5, then 6-10, etc.. Then I want to convert these bits to their corresponding decimal value.

but all the binary functions I found in autoit only work with full bytes, and do not let me get smaller sections of bits, like "BinaryMid()" that "Extracts a number of bytes from a binary variant"

Can anyone tell me if this is possible to do and how? and also if there is a function to convert those bits to/from decimals?

I'm not that familiar with dealing directly with binary, so any help or tips will be very appreciated

Context:

I'm trying to make a function to encode/decode any given binary data into/from a given array of characters. just like Base64 but using different bases then [a-z A-Z 0-9 `+/=` ]. My approach currently is to figure out how many bits of binary I can encode with each character, read those bits and convert them to a decimal number, then I will use that number as an index and take the character at that index from the character array and add it to the result string.

I'm aware that there may be some padding needed.

##### Share on other sites

This is possible to do with the DllStruct- and the Bit-commands. If you want 5 bit blocks you can store 5 bytes at a time from your binary data into a 5 byte structure (40 bits). Then you can handle each of the 5 bit blocks one by one. 8 blocks in a total.

The CPU only handles 1, 2, 4 or 8 (64 bit CPU) bytes at a time. It does not handle 5 or 7 bytes. To handle 5 bytes you can for example split them up in 2 bytes, 2 bytes and 1 byte. If some of the 5 bit blocks have bits spread over byte 2 and 3 or byte 4 and 5 then you must deal with it.

To do all this you will need to be able to handle binary data. And you must be able to convert from binary to decimal numbers in your head. At least for the first 8 bits.

If you have large amounts of binary data AutoIt is not the fastest programming language. You'll need a compiled language.

What's the purpose for this? I do not think it's possible to make better algorithms for simple encoding/decoding, than those that already exists.

##### Share on other sites
14 minutes ago, LarsJ said:

This is possible to do with the DllStruct- and the Bit-commands. If you want 5 bit blocks you can store 5 bytes at a time from your binary data into a 5 byte structure (40 bits). Then you can handle each of the 5 bit blocks one by one. 8 blocks in a total.

The CPU only handles 1, 2, 4 or 8 (64 bit CPU) bytes at a time. It does not handle 5 or 7 bytes. To handle 5 bytes you can for example split them up in 2 bytes, 2 bytes and 1 byte. If some of the 5 bit blocks have bits spread over byte 2 and 3 or byte 4 and 5 then you must deal with it.

To do all this you will need to be able to handle binary data. And you must be able to convert from binary to decimal numbers in your head. At least for the first 8 bits.

If you have large amounts of binary data AutoIt is not the fastest programming language. You'll need a compiled language.

What's the purpose for this? I do not think it's possible to make better algorithms for simple encoding/decoding, than those that already exists.

Thanks for the reply LarsJ,  I did some searching before and tried the bit shifting method but it got complicated fast, I will only deal with small binary data so I instead chose to convert the binary to 0/1 strings and handling that instead, pretty dirty and slow, I Know.

Are there any existing encoding functions/libraries that can encode to an arbitrary list of characters? this is exactly what I need.

This function is for a little encryption based game I'm doing for fun, I know autoit may not be the best fit but it's fast to write & easy to use, and it's just a small project.

##### Share on other sites

If you Google in this way I think you'll find something:

`encode decode site:autoitscript.com`

## Create an account

Register a new account

• ### Similar Content

• By rot3r
Hi friends,
I'm using json.au3 to register some data in my server, everything work fine except the data that i send in utf8 format like names saved as ????? .
i think something mess with inetget cause this problem

• Func _Binary(\$Int) ;Uncomment To Only Accept Integers #cs If IsInt(\$Int) = 0 Then Return 0 EndIf #ce If \$Int < 0 Then ;Negative Numbers Will Break The Function Return 0000 EndIf Local \$Integer = \$Int Dim \$Bin[1] = [Mod(\$Integer, 2)] Local \$Counter = 1 Do \$Integer = Floor(\$Integer / 2) _ArrayAdd(\$Bin, Mod(\$Integer, 2)) Until \$Integer = 0 _ArrayReverse(\$Bin) ;Reverses The Array Because As Is, The Product Is Backwards ;A Loop To Remove Any Preceding 0's or Add 0's To Keep At Least Four Digits Select Case \$Int <= 1 \$Integer = "00" & _ArrayToString(\$Bin, "") Case \$Int = 2 Or \$Int = 3 \$Integer = "0" & _ArrayToString(\$Bin, "") Case \$Int >= 8 \$Integer = StringTrimLeft(_ArrayToString(\$Bin, ""), 1) Case Else \$Integer = _ArrayToString(\$Bin, "") EndSelect ;You Can Comment It Out Without Anything Else Having A Problem Return \$Integer EndFunc I made this because I was writing something that I could use to play with Bitwise Operations and using Binary() by itself wasn't helping.
It's very basic and will only take positive integers because that's all I needed but I'm sure with a little tweaking you could make it fit with negative and float types.
It returns a string essentially but doesn't pose a problem when just changing numbers into binary digits.
Example: If you were to do _Binary(5) you would get "0101" and _Binary(8) would return "1000"
Between this last sentence and here I've changed this about a half dozen times to refine it a bit because without it checking if your number is < 0 it would break if a negative number was inserted and it wouldn't even have a problem if you put in Float Values, Regular or Special Characters but that negative value will do the trick lol.
Anyway, I hope someone finds some use of this and thank you for reading!
-Pick

• Hi AutoIt Programmers, i wanna figure out how to use Binary functions in C# like:

BinaryMid
BinaryLen
IsBinary and other basic ones were too ez, but those two were hard to noob like me.

• By UEZ
I'm using very often binary strings to read images, sounds, etc. directly for memory to avoid any file installation.

Here a small tool to convert any file to a base64 string incl. compression option to use it within your script to avoid usage of local disk (file install)! It will also created the needed functions!

Code is too long now for the code box -> Pastebin.com

Code will run on 3.3.11.4 or higher versions only !

Thanks to:
Ward for the _Base64Encode() / LZMAT / MsgBoxEx() functions.
trancexx for the LZNTCompress / LZNTDecompress and _Base64Decode() functions!
wraithdu for bug fixing and advancing the code!

Just load any file(s), convert it and use it in your code! Checkout tooltips when hovering some controls... 😉

Example using some images from ..\AutoIt3\Examples\GUI folder.

Display images from memory in controls:

Play a wave file from memory:

Display Font from Memory:

Display embedded ico file in system tray:
#include <GDIPlus.au3> #include <WindowsConstants.au3> _GDIPlus_Startup() Global \$hBmp = _GDIPlus_BitmapCreateFromMemory(_AutoIt_Icon()) ;load ico and convert it to a GDI+ bitmap ;convert bitmap to HIcon Global \$hIcon = _GDIPlus_HICONCreateFromBitmap(\$hBmp) _WinAPI_TraySetHIcon(\$hIcon) Global \$hGUI = GUICreate("Display embedded ico file in tray", 320, 50) GUICtrlCreateLabel("Look at tray icon", 0, 0, 300, 200) GUICtrlSetFont(-1, 30) GUISetState() Do Until GUIGetMsg() = -3 _GDIPlus_BitmapDispose(\$hBmp) _WinAPI_DestroyIcon(\$hIcon) _GDIPlus_Shutdown() Exit Func _WinAPI_TraySetHIcon(\$hIcon) ;function by Mat Local Const \$tagNOTIFYICONDATA = _ "dword Size;" & _ "hwnd Wnd;" & _ "uint ID;" & _ "uint Flags;" & _ "uint CallbackMessage;" & _ "ptr Icon;" & _ "wchar Tip[128];" & _ "dword State;" & _ "dword StateMask;" & _ "wchar Info[256];" & _ "uint Timeout;" & _ "wchar InfoTitle[64];" & _ "dword InfoFlags;" & _ "dword Data1;word Data2;word Data3;byte Data4[8];" & _ "ptr BalloonIcon" Local Const \$TRAY_ICON_GUI = WinGetHandle(AutoItWinGetTitle()), \$NIM_ADD = 0, \$NIM_MODIFY = 1, \$NIF_MESSAGE = 1, \$NIF_ICON = 2, \$AUT_WM_NOTIFYICON = \$WM_USER + 1, \$AUT_NOTIFY_ICON_ID = 1 Local \$tNOTIFY = DllStructCreate(\$tagNOTIFYICONDATA) DllStructSetData(\$tNOTIFY, "Size", DllStructGetSize(\$tNOTIFY)) DllStructSetData(\$tNOTIFY, "Wnd", \$TRAY_ICON_GUI) DllStructSetData(\$tNOTIFY, "ID", \$AUT_NOTIFY_ICON_ID) DllStructSetData(\$tNOTIFY, "Icon", \$hIcon) DllStructSetData(\$tNOTIFY, "Flags", BitOR(\$NIF_ICON, \$NIF_MESSAGE)) DllStructSetData(\$tNOTIFY, "CallbackMessage", \$AUT_WM_NOTIFYICON) Local \$aRet = DllCall("shell32.dll", "int", "Shell_NotifyIconW", "dword", \$NIM_MODIFY, "ptr", DllStructGetPtr(\$tNOTIFY)) If (@error) Then Return SetError(1, 0, 0) Return \$aRet[0] <> 0 EndFunc ;==>_Tray_SetHIcon ;Code below was generated by: 'File to Base64 String' Code Generator v1.12 Build 2013-05-17 Func _AutoIt_Icon(\$bSaveBinary = False, \$sSavePath = @ScriptDir) Local \$AutoIt_Icon \$AutoIt_Icon &= 'AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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JaU25WSj92dmpfhcmxooUpCPH/c29r/zs/Q/93e3/+DfXmxAAAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHx2cRB+eHMSenNuFgAAAAAAAAAAWVJMYZmUkcJnYFuiZV5ZKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAAA//+sQRAfrEEAD6xBAAesQYADrEGAA6xBAAGsQQABrEEAAaxBgAOsQYABrEHAAKxB4ACsQfAArEH8YaxB//esQQ==' Local \$bString = Binary(_Base64Decode(\$AutoIt_Icon)) If \$bSaveBinary Then Local \$hFile = FileOpen(\$sSavePath & "\AutoIt.ico", 18) FileWrite(\$hFile, \$bString) FileClose(\$hFile) EndIf Return \$bString EndFunc ;==>_AutoIt_Icon Func _Base64Decode(\$sB64String) Local \$aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", \$sB64String, "dword", 0, "dword", 1, "ptr", 0, "dword*", 0, "ptr", 0, "ptr", 0) If @error Or Not \$aCrypt[0] Then Return SetError(1, 0, "") Local \$bBuffer = DllStructCreate("byte[" & \$aCrypt[5] & "]") \$aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", \$sB64String, "dword", 0, "dword", 1, "struct*", \$bBuffer, "dword*", \$aCrypt[5], "ptr", 0, "ptr", 0) If @error Or Not \$aCrypt[0] Then Return SetError(2, 0, "") Return DllStructGetData(\$bBuffer, 1) EndFunc ;==>_Base64Decode

Display a transparent image in GUI:

More examples:
Playing SWF file (source code is too large for codebox)
AutoIt Windows Screenshooter
Play MP3 from mem using FMOD
Toolbar from mem
Play a GIF anim from mem
Load animated cursor (.ani) from memory

Br,
UEZ

History:

• By Fenzik
Hello All!
i suggest to set default encoding in Scite4 for Autoit 3 to UTF 8 with Bom encoding, format recommended also in Autoit Help.
In last editor version, when i open new script, for example Czech characters (č, ř, ž) aren't correct.
So when i change Encoding to UTF 8 with Bom from Default Code page property state, everithing seems to be OK.
Thank you and sorry for potentialy duplicated content.
Fenzik
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...