Jump to content

Binary to Hex Conversion


Recommended Posts

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

That's what I thought too, I tried a bunch of different ways of doing it.

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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Developers

$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)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

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]
Link to comment
Share on other sites

I like that one! Now on to the rest of my program.... Mwahahaha!!

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]
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...