Sign in to follow this  
Followers 0
Kickassjoe

new Function by meh?

11 posts in this topic

i know that there is _stringtohex and _hextostring in v3... but i was wanting to convert numbers... not letters... and _stringtohex wasnt working for meh

I am working on a function that converts numbers to hex and hex to numbers (my friend just taught me the math of it because i am learning to hex edit in diablo2) and was wondering if this was already implemented or soon to be implemented in the beta version (if someone has done it before) it will be written in au3 not c++... maybe just make it a UDF?

I will have it done tomorrow i think... if it works how i think its gonna work... if it is a UDF... i dont really have any experience with that... so could someone help me make it a UDF or make it a UDF for me once i give you the code? and maybe help me clean it up a lil bit? its a little bit confusing as it is (like if you were to read the source)


What goes around comes around... Payback's a bitch.

Share this post


Link to post
Share on other sites



Hex() and Dec()? (In the help file)

Share this post


Link to post
Share on other sites

aww... everyone always has to ruin my fun : (


What goes around comes around... Payback's a bitch.

Share this post


Link to post
Share on other sites

can anyone help me figure this out?

#include <string.au3>

;******************************************************************
;*Author: KickassJoe A.K.A. LLcoolJoe <Jrankin3@hotmail.com>      *
;*                                                                *
;*  Program is used for converting Decimal values TO Hex values   *
;*                                                                *
;*                                                                *
;******************************************************************


HotKeySet("h", "HextoDecimal")
HotKeySet("{esc}", "Terminate")

While 1
    Sleep(1000)
WEnd

Func HextoDecimal()
$Decimal = InputBox("Hex to Decimal converter", "What Decimal would you like to be converted to Hex?")
$StartNumber = $Decimal; for later use

If $Decimal >= 16 Then        ;if the number from the user is greater than 16
    $Decimal = $Decimal / 16  ;divide it by 16, because hex is a base-16 system (or I think thats y)
    $numboftimesdivided = 16  ;= 16 because Hex is a base-16 system
EndIf

If $Decimal >= 16 Then
    $Decimal = $Decimal / 16
    $numboftimesdivided = $numboftimesdivided * 16
EndIf

If $Decimal >= 16 Then
    $Decimal = $Decimal / 16
    $numboftimesdivided = $numboftimesdivided * 16
EndIf

Select
    Case $Decimal < 10
        $Firstanswerisnumber = $Decimal;if it is less then 10... keep the numb as that var
        
Case $Decimal >= 10 AND $Decimal < 11
        $Firstanswerisletter = a;firstanswer is the letter a... and so on
        
Case $Decimal >= 11  AND $Decimal < 12
        $Firstanswerisletter = b

Case $Decimal >= 12  AND $Decimal < 13
        $Firstanswerisletter = c

Case $Decimal >= 13  AND $Decimal < 14
        $Firstanswerisletter = d

Case $Decimal >= 14  AND $Decimal < 15
        $firstanswerisletter = e

Case $Decimal >= 15  AND $Decimal < 16
        $Firstanswerisletter = f
        EndSelect


$numbforminusing = $Decimal * $numboftimesdivided ;used for right after i multiply

$Decimal = $Decimal * $numboftimesdivided ;divide!

$Decimal = $StartNumber - $Decimal ;take the number you started with at the beginning


If $Decimal >= 16 Then
    $Decimal = $Decimal / 16
    $numboftimesdivided = 16
EndIf

If $Decimal >= 16 Then
    $Decimal = $Decimal / 16
    $numboftimesdivided = $numboftimesdivided * 16
EndIf

If $Decimal >= 16 Then
    $Decimal = $Decimal / 16
    $numboftimesdivided = $numboftimesdivided * 16
EndIf

Select
    Case $Decimal < 10
        $Secondanswerisnumber = $Decimal ;if it is less then 10... keep the numb as that var
        
Case $Decimal >= 10 AND $Decimal < 11
        $Secondanswerisletter = a ;secondanswer is the letter a
        
Case $Decimal >= 11  AND $Decimal < 12
        $Secondanswerisletter = b

Case $Decimal >= 12  AND $Decimal < 13
        $Secondanswerisletter = c

Case $Decimal >= 13  AND $Decimal < 14
        $Secondanswerisletter = d

Case $Decimal >= 14  AND $Decimal < 15
        $Secondanswerisletter = e

Case $Decimal >= 15  AND $Decimal < 16
        $Secondanswerisletter = f
        EndSelect



$Decimal = $Decimal * $numboftimesdivided

$Decimal = $numbforminusing - $Decimal


If $Decimal >= 16 Then
    $Decimal = $Decimal / 16
    $numboftimesdivided = 16
EndIf

If $Decimal >= 16 Then
    $Decimal = $Decimal / 16
    $numboftimesdivided = $numboftimesdivided * 16
EndIf

If $Decimal >= 16 Then
    $Decimal = $Decimal / 16
    $numboftimesdivided = $numboftimesdivided * 16
EndIf

Select
    Case $Decimal < 10
        $Thirdanswerisnumber = $Decimal
        
Case $Decimal >= 10 AND $Decimal < 11
        $Thirdanswerisletter = A
        
Case $Decimal >= 11  AND $Decimal < 12
        $Thirdanswerisletter = B

Case $Decimal >= 12  AND $Decimal < 13
        $Thirdanswerisletter = C

Case $Decimal >= 13  AND $Decimal < 14
        $Thirdanswerisletter = D

Case $Decimal >= 14  AND $Decimal < 15
        $Thirdanswerisletter = E

Case $Decimal >= 15  AND $Decimal < 16
        $Thirdanswerisletter = F
EndSelect



If IsDeclared($Firstanswerisnumber) Then
    $Firstanswer = $Firstanswerisnumber
    Int($Firstanswerisnumber)
Else
    $Firstanswer = $Firstanswerisletter
EndIf

If IsDeclared($Secondanswerisnumber) Then
    $Secondanswer = $Secondanswerisnumber
    Int($Secondanswerisnumber)
Else
    $Secondanswer = $Secondanswerisletter
EndIf

If IsDeclared($Thirdanswerisnumber) Then
    $Thirdanswer = $Thirdanswerisnumber
    Int($Thirdanswerisnumber)
Else
    $Thirdanswer = $Thirdanswerisletter
EndIf

$Finalanswer = $Firstanswer & $Secondanswer & $Thirdanswer

MsgBox(0, "Decimal to Hex Converter",_
"Your Decimal has been converted to Hex!" & @LF _
& "Your Hex is:  " & $Finalanswer)
EndFunc


Func Terminate()
    Exit
    EndFunc

P.S. anyone think thats good for 2 months programming in autoit? (2 months experience... it didnt take me 2 months)

what is wrong??? this is regular v3 not beta

$Firstanswerisletter = a;firstanseris the lettter a... and so on

$Firstanswerisletter = ^ ERROR

Error:Unknown function name.


What goes around comes around... Payback's a bitch.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Strings have to be surrounded by quotes... also you'll get an error on one of your underscores, there must be a space before it.

Edit:

Your logic is a bit off as well

Case $Decimal >= 10 AND $Decimal < 11

Isn't that the same as:

Case $Decimal = 10
?

Also, you may want to ask this to be moved out of the Developers forum since you've already found that there were functions for this.

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Yes... the two would be the same... but it wont always be a whole number... does that matter?

Case $Decimal = 10

what if $Decimal = 10.438457? would that still work?

o wow... i totally misread that lol... thought it said parenthesis

Edited by Kickassjoe

What goes around comes around... Payback's a bitch.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

and... maybe i can get it so that it can return more than 8 digits of Hex :whistle:

Edited by Kickassjoe

What goes around comes around... Payback's a bitch.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Your code shows you understand the math somewhat.

http://en.wikipedia.org/wiki/Number_base

Read that article and rethink the way you coded that. I suggest you write some psuedo code and pm it to me. I will help you with this function. Its not terribly useful since its already made, but its a good learning tool.

Edited by Wus

Share this post


Link to post
Share on other sites

Example: 709

709/16 = 44,3125 (bigger than 16)

44,3125/16 = 2,76953125 (smaller than 16)

--> the first number is 2

2 * 16 * 16 = 512

709 - 512 = 197

197/16 = 12,3125 (smaller than 16)

--> the second number is C

12 * 16 = 192

197 - 192 = 5

--> the last number is 5

Total 2C5 = 709

thats an example that i used to get the math right... would that be the most effective way of doing it? / least complicated?

and what is pseudo??? new language for me? yea right lol... the only languages im even close to being good at are autoit3 and pascal...


What goes around comes around... Payback's a bitch.

Share this post


Link to post
Share on other sites

In less than the amount of code you show in post #4, you should be able to write a base 2 <-> 64 converter, not just a base 10 <-> 16. You might want to keep that in mind while writing a ton of code to convert between two bases.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I am tired this night and didnt feel like thinking so I adapter the following from http://www.planet-source-code.com/vb/scrip...60&lngWId=6

Its the standard old way.

$dec = 100
$out = "decimal number is:"& $dec & @CRLF
$hex = _dec2hex($dec)
$out &= "hex number is: " & $hex

msgbox( 48, "Test output for _dec2hex()", $out)

Func _dec2hex( $decnum)
    Local $hexnum = ""
    Local $tempval
    While $decnum <> 0
        $tempval = Mod( $decnum, 16)
        If $tempval > 9 Then
            $tempval = chr($tempval + 55)
        EndIf
        $hexnum = $tempval & $hexnum
        $decnum = int($decnum / 16)
        If $decnum < 16 AND $decnum <> 0 Then
            If $decnum > 9 Then
            $decnum = chr($decnum + 55)
            EndIf
            $hexnum = $decnum & $hexnum
            $decnum = 0 
        EndIf
    WEnd
    Return $hexnum
EndFunc

Ill leave the hex to dec ( _hex2dec() ) to you as a challenge.

PM me with questions, the dev forum isnt really the place for this.

The idea of this func was to divide by 16.

So naturally the concept of the mirror func is to multiply by 16.

dec = 0;

while( theres a hex digit available )

dec = dec* 16 + decimal representation( hex_digit )

wend

return dec

Also google horners method. If you understand the math of horners method everything will be a lil clearer.

Edited by Wus

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