Sign in to follow this  
Followers 0
kylomas

Display binary representation of a byte

11 posts in this topic

#1 ·  Posted (edited)

Happy 2011,

Does anyone know how to display the binary representation of a byte?

e.g. int=8 might be "0000 0000 0000 1000" (assuming 2 byte int)

Thanks,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites



kylomas,

A quick search of the forum (the Search facility is at top right of the page :P ) found this. It might be useful. :x

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

These are always my preferred functions. None of the others on the forums really compare :x

there is also the reverse here:

I wish WBD was here more often.

Share this post


Link to post
Share on other sites

Just out of curiosity, do I get this right?

$sString = "The quick brown fox jumps over the lazy dog"
$iStringLength = StringLen($sString)
For $i = 1 To $iStringLength
    $sChar = StringMid($sString, $i, 1)
    ConsoleWrite($sChar & @tab)
    $sBin = _ToBase(Dec(StringReplace(StringToBinary($sChar), "0x", "")), 2, 8)
    ConsoleWrite($sBin & @crlf)
Next

; by WideBoyDixon
; http://www.autoitscript.com/forum/topic/93742-converting-decimal-numbers-to-another-base/page__view__findpost__p__673358
Func _ToBase($iNumber, $iBase, $iPad = 1)
    Local $sRet = "", $iDigit
    Do
        $iDigit = Mod($iNumber, $iBase)
        If $iDigit < 10 Then
            $sRet = String($iDigit) & $sRet
        Else
            $sRet = Chr(55 + $iDigit) & $sRet
        EndIf
        $iNumber = Int($iNumber / $iBase)
    Until ($iNumber = 0) And (StringLen($sRet) >= $iPad)
    Return $sRet
EndFunc   ;==>_ToBase

Share this post


Link to post
Share on other sites

Melba23,

Thanks, could'nt find the right search terms to pop these results...

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Just out of curiosity, do I get this right?

I think this is what you intended:

$sString = "The quick brown fox jumps over the lazy dog"
$iStringLength = StringLen($sString)
For $i = 1 To $iStringLength
    $sChar = StringMid($sString, $i, 1)
    ConsoleWrite($sChar & @TAB & Asc($sChar) & @TAB)
    $sBin = _ToBase(Asc($sChar), 2, 8)
    ConsoleWrite($sBin & @crlf)
Next

; by WideBoyDixon
; http://www.autoitscript.com/forum/topic/...her-base/page__view__findpost_
Func _ToBase($iNumber, $iBase, $iPad = 1)
    Local $sRet = "", $iDigit
    Do
        $iDigit = Mod($iNumber, $iBase)
        If $iDigit < 10 Then
            $sRet = String($iDigit) & $sRet
        Else
            $sRet = Chr(55 + $iDigit) & $sRet
        EndIf
        $iNumber = Int($iNumber / $iBase)
    Until ($iNumber = 0) And (StringLen($sRet) >= $iPad)
    Return $sRet
EndFunc   ;==>_ToBase

To convert AutoIt's binary representation to binary bits there is this function:

of course I'm trying to guess what you were trying to do, I may be wrong.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

All functions deliver the same result :x ... hmmmm, how about unicode characters? E.g. feeding a Russian Л to the function results in a console output like "? 00111111", П the same "? 00111111".

$sString = "The quick brown fox jumps over the lazy dog"
$iStringLength = StringLen($sString)
For $i = 1 To $iStringLength
    $sChar = StringMid($sString, $i, 1)
    ConsoleWrite($sChar & @tab)
    $sBin = _ToBase(Dec(StringReplace(StringToBinary($sChar), "0x", "")), 2, 8)
    ConsoleWrite($sBin & @tab)
    $sBin2 = BinaryToBits(StringToBinary($sChar))
    ConsoleWrite($sBin2 & @crlf)
Next

$iStringLength = StringLen($sString)
For $i = 1 To $iStringLength
    $sChar = StringMid($sString, $i, 1)
    ConsoleWrite($sChar & @TAB)
    $sBin = _ToBase(Asc($sChar), 2, 8)
    ConsoleWrite($sBin & @crlf)
Next

; by WideBoyDixon
; http://www.autoitscript.com/forum/topic/93742-converting-decimal-numbers-to-another-base/page__view__findpost__p__673358
Func _ToBase($iNumber, $iBase, $iPad = 1)
    Local $sRet = "", $iDigit
    Do
        $iDigit = Mod($iNumber, $iBase)
        If $iDigit < 10 Then
            $sRet = String($iDigit) & $sRet
        Else
            $sRet = Chr(55 + $iDigit) & $sRet
        EndIf
        $iNumber = Int($iNumber / $iBase)
    Until ($iNumber = 0) And (StringLen($sRet) >= $iPad)
    Return $sRet
EndFunc   ;==>_ToBase

; by SkinnyWhiteGuy
; http://www.autoitscript.com/forum/topic/93345-binary-digits-stuff/page__p__670985#entry670985
Func BinaryToBits($bin)
    Local $output
    For $i = 1 To BinaryLen($bin)
        For $j = 7 To 0 Step -1
            $output &= BitAND(BitShift(BinaryMid($bin, $i, 1), $j), 1)
        Next
    Next
    Return $output
EndFunc
Edited by KaFu

Share this post


Link to post
Share on other sites

Mat,

Precicely what I was looking for, thanks.

Just as an aside, I appreciate the patients all members of this forum have shown. I am an old MVS (mainframe operating system) guy trying to figure out this Windows thing.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

All functions deliver the same result :x ... hmmmm, how about unicode characters? E.g. feeding a Russian Л to the function results in a console output like "? 00111111", П the same "? 00111111".

I can't seem to get SciTE to let me use russian characters... So I copy + pasted this: > Привет мир! < (Got from google translate). It recognizes that the characters are different and seems to give the right results... Unfortunately I don't know russian, so can't tell you if it's doing what it should. Only real changes are that it uses AscW and pads it to 16 bits. Ascii characters still give the same results though :P

$sString = ClipGet()
$iStringLength = StringLen($sString)
For $i = 1 To $iStringLength
    $sChar = StringMid($sString, $i, 1)
    ConsoleWrite($sChar & @TAB & AscW($sChar) & @TAB)
    $sBin = _ToBase(AscW($sChar), 2, 16)
    ConsoleWrite($sBin & @crlf)
Next

; by WideBoyDixon
; http://www.autoitscript.com/forum/topic/...her-base/page__view__findpost_
Func _ToBase($iNumber, $iBase, $iPad = 1)
    Local $sRet = "", $iDigit
    Do
        $iDigit = Mod($iNumber, $iBase)
        If $iDigit < 10 Then
            $sRet = String($iDigit) & $sRet
        Else
            $sRet = Chr(55 + $iDigit) & $sRet
        EndIf
        $iNumber = Int($iNumber / $iBase)
    Until ($iNumber = 0) And (StringLen($sRet) >= $iPad)
    Return $sRet
EndFunc   ;==>_ToBase

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

AscW() did the trick :P. Here's a good source for digging deeper.

Edit: :x, the author called the article "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)"

Edited by KaFu

Share this post


Link to post
Share on other sites

I read it a while back, I think it was linked to from here by someone else a while ago. The author is right of course :x

Share this post


Link to post
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
Sign in to follow this  
Followers 0