Jump to content

Compare digit to it's hex


 Share

Recommended Posts

Hello,

Why this works?

Local $digit = 99120
Local $hex = 0x18330
If $digit = $hex Then
 ConsoleWrite("Yes it is" & @LF)
Else
 ConsoleWrite("Nop it isn't" & @LF)
EndIf

i understand that comparing digit to it's hex is like comparing it to itself but then why this won't work?

Local $digit = 99120
;Local $hex = 0x18330
If $digit = Hex($digit) Then
 ConsoleWrite("Yes it is" & @LF)
Else
 ConsoleWrite("Nop it isn't" & @LF)
EndIf

Actually i knows why it doesn't work but normally it should work since we are comparing a int to it's hex which means to itself as in the first example.
Hex will return a string represantation of the integer in hexadecimal. Wouldn't be better to return the exact hex value like it should be.

I mean to return this: 0x18330 other than 000018330.

Link to comment
Share on other sites

Hex returns a string, it's the hexidecimal representation of the number, it does not return it in a hex format. This will demonstrate what it's doing and why it's not working.

Local $digit = 99120
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Hex($digit) = ' & Hex($digit) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : Number(Hex($digit)) = ' & Number(Hex($digit)) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
If $digit = Hex($digit) Then
    ConsoleWrite("Yes it is" & @LF)
Else
    ConsoleWrite("Nope it isn't" & @LF)
EndIf
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BTW, you got lucky that you hit on a number that when converted to Hex is still all numbers, try making the comparison using 160 decimal, and then you're not even comparing it against a number, although Number ("A0") will come out to 0.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I don't know if you got the point of this thread. I am not saying that Hex returns a number, i know very well what Hex returns.
I am saying that, in my poor opinion, Hex should return a string in a hex format since we are converting an integer in a Hex so we can use it directly other that having to do workarrounds.

So it would be better to work like this:

Local $digit = 99120
Local $hex = 0x18330
If $digit = Hex($digit) Then
 ConsoleWrite("Yes it is" & @LF)
Else
 ConsoleWrite("Nop it isn't" & @LF)
EndIf

other than this:

Local $digit = 99120
Local $hex = 0x18330
If $digit = "0x" & Hex($digit) Then
 ConsoleWrite("Yes it is" & @LF)
Else
 ConsoleWrite("Nop it isn't" & @LF)
EndIf

I faced this problem while i was reading memory and i wanted to turn int to hex and then add them to the memory. After converting an int to a hex i had to turn it into hex format to be able to work with which i find pointless.

Thank you anyway.

Edited by AutID
Link to comment
Share on other sites

Not sure you realy understand what hex is.. It is simply another method of counting, base 16 instead of base 10.

Your first example is doing it correct, you are comparing an Integer to an Integer. Although your variable $hex is declared with hex numbering, its content is the same as your variabe $digit. The correct way would be compare this as you did in your first post without the Hex(). (otherwise you need to use Hex() around both $digit and $hex and compare the resulting strings, but what is the point in that?)

Link to comment
Share on other sites

As I am saying in the last post, I am reading memory. To be able to read static addresses you have to add bytes to the offsets.

Bytes in my case are returned into digits and I need to add them in hex format.

Using offset + Hex($digit) won't work.

It works like this offset + "0x" + Hex($digit).

So either I add them as the second solution does either I make a small function to return a hex of a digit in hex format.

So in other words what I am saying is that in my opinion Hex function should return hexadecimal of an int into hex format other than a string representing its hex.

Anyway I am not the who created those functions and I am not judging the dev's decisions. They must know something more than I do.

Edited by AutID
Link to comment
Share on other sites

isnt 0x just the prefix, not a part of the base16 representation?

you would prefer that autoit expect that you need to execute this string as hex with a language that uses 0x and prefix it as such, rather than just return the actual representation of the int in hex?

*what if i am going pixel by pixel through an image?  i only need 0x at the very beginning and would have to remove it from all subsequent conversions to append the value.

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

AutID,

This doesn't make sense at all. Integers are itegers, not strings.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

AutID,

 

This doesn't make sense at all. Integers are itegers, not strings.

That's what I am saying. I would prefere that hex function returns a hexadecimal of an integer into hex format other than a string representing the hexadecimal of an integer as it does now according to the help files.

Edit: To everyone, this is what i prefere, not what is correct. Don't get me wrong.

Edited by AutID
Link to comment
Share on other sites

 In this example i convert 3 values to Hex for the purpose of drawing a big pink rectangle.  So currently I get to add a 0x one time at the beginning when i want to combine those values into something the interpreter likes.  With your solution I have to remove the prefix from every variable except $B, and if I have another function that uses RGB instead of BGR I have to keep variables for the prefixed and non-prefixed, or remember to put it on $R and remove it from $B.

$R = hex(255 , 2)
$G = hex(100 , 2)
$B = hex(100 , 2)

$color = "0x" & $B & $G & $R

for $i = 400 to 800
    for $j = 400 to 800
    SetPixel("" , $i , $j , $color)
next
next

    Func SetPixel($handle, $x, $y, $color)
    Local $dc
    $dc = DllCall("user32.dll", "int", "GetDC", "hwnd", $handle)
    DllCall("gdi32.dll", "long", "SetPixel", "long", $dc[0], "long", $x, "long", $y, "long", $color)
    DllCall("user32.dll", "int", "ReleaseDC", "hwnd", 0, "int", $dc[0])
    Return 1
EndFunc   ;==>SetPixel

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

As I am saying in the last post, I am reading memory. To be able to read static addresses you have to add bytes to the offsets.

Bytes in my case are returned into digits and I need to add them in hex format.

Using offset + Hex($digit) won't work.

It works like this offset + "0x" + Hex($digit).

So either I add them as the second solution does either I make a small function to return a hex of a digit in hex format.

So in other words what I am saying is that in my opinion Hex function should return hexadecimal of an int into hex format other than a string representing its hex.

Anyway I am not the who created those functions and I am not judging the dev's decisions. They must know something more than I do.

 

So why do you have to convert the number to hex if you only going to to add it to another integer? It will give you the same result if not "converting" $digit to hex... Can you give a more detailed description or example of what you are trying to do?

As i tried to explain, hex is just another method of representing a number. If you place 255 apples in one basket and 0xFF apples in another, you would see both baskets contains exactly the same amount of apples. (hex(255) = 0xFF). So if you add 255 or 0xFF apples to your offset it will give the same result?

Edited by Geir1983
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...