Jump to content
Sign in to follow this  
AutoItKing

Binary to Hex Conversion

Recommended Posts

AutoItKing

I've tried Hex(), obviously... But it gives me wacko stuff. 0101 1010 should output 5A. But it doesn't. 101 should go to 5, yet it goes to 65... Huh? Any help?


http://www.autoitking.co.nr Site is DOWN | My deviantART | No Topic Topic - Don't do it!-------------------- UDF's/Scripts:AutoIt: [BenEditor 3.6] [_ShutDown()]PHP: [CommentScript]Web Based AutoIt: [MemStats] [HTML to AU3] [User LogIn and SignUp script]

Share this post


Link to post
Share on other sites
the_lord_mephy

That's because the Hex function is seeing your number as an integer, so instead of seeing 101 as binary, it's seeing the number one-hundred and one. There might be some sort of notation you have to put binary numbers in. I'll keep looking.


My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Share this post


Link to post
Share on other sites
PsaltyDS

I've tried Hex(), obviously... But it gives me wacko stuff. 0101 1010 should output 5A. But it doesn't. 101 should go to 5, yet it goes to 65... Huh? Any help?

I stops at the break (space) and reads it as decimal, not binary: 10110 = 0x65

AuotIt does not seem to support the notation "0b01011010" for a binary nuber. (See help file under Data Types)

:rolleyes:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Jos

$BinIn = "01011010"
$Bits = StringSplit($BinIn,"")
$Dec = 0
For $x = $Bits[0] To 1 Step -1
    $dec +=  (2^($Bits[0]-$x)) * $Bits[$x]
Next
$hex = Hex($dec)
ConsoleWrite("bin:" & $BinIn & "  Dec:" & $dec & "   Hex:" & $Hex & @CRLF)


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
the_lord_mephy

lol I shoulda refreshed i just made UDF for it :rolleyes:

Func BinaryToHex($bin)
    Dim $hex, $thex, $num
    $bins = StringSplit($bin, " ")
    For $i = 1 to $bins[0]
        If StringLen($bins[$i]) <> 4 Then
            MsgBox(0, "", $bins[$i])
            Return -1
        EndIf
        $nums = StringSplit($bins[$i], "")
        $thex = 0
        If $nums[1] = 1 Then $thex = $thex + 8
        If $nums[2] = 1 Then $thex = $thex + 4
        If $nums[3] = 1 Then $thex = $thex + 2
        If $nums[4] = 1 Then $thex = $thex + 1
        $num = $num & StringRight(Hex($thex), 1)
    Next
    Return $num
EndFunc

MsgBox(0, "", BinaryToHex("0101 1010"))


My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

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  

×