Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Hexadecimal to Decimal Conversion Tool


  • Please log in to reply
9 replies to this topic
Maelgwyn
  • Guests
  • Last active:
  • Joined: --
Just a simple code for easy conversion between hexadecimally notated and decimally notated digits. Particularly useful around the PostMessage and SendMessage functions.

I'm not sure if this is somehow obsolete or has already been done but if not, it should help.

; Not a complicated script but very useful
;_______________________________________________________________________________
;  Usage: Follow the prompts and enter either a hexadecimal value, ie, an or many
; integer/s or alphabetical value/s (Between A-F) proceeded by the prefix, 
; 0x (Zero Multiplied By). For example, 0x8 OR/ 0xFFACD. Or a ; ; decimal ;value 
; such as 987 or 250.  
;_______________________________________________________________________________
; 
;

;---------------------------------------------
;HOTKEY- (Feel free to change to your liking)
;---------------------------------------------

#p::

;---------------------------------------------
;HEX TO DECIMAL
;---------------------------------------------

MsgBox 4, Hexadecimal Conversion Tool,Hexadecimal to Decimal?

IfMsgBox, No

Gosub, Label1 

IfMsgBox, Yes

InputBox, Hex1, Enter a Hexadecimal value

SetFormat, integer, decimal
Lolly -= 0

MsgBox, 0, Hexadecimal Conversion Tool, The Decimal value is %Hex1%

Exit

;---------------------------------------------
;DECIMAL TO HEX
;---------------------------------------------

Label1:

MsgBox, 4, Hexadecimal Conversion Tool, Decimal to Hexidecimal?

IfMsgBox, No

Exit

InputBox, Dec1, Enter a Decimal value

SetFormat, integer, hex
Lollies += 0

MsgBox, 0, Hexadecimal Conversion Tool, The Hexadecimal value is %Dec1%


{Guest}
  • Guests
  • Last active:
  • Joined: --
Not sure what's happened here Maelgwyn but this script appears to be some form of weird hybrid. I've repaired it and it seems to be functional now. I'll just post the new script to save time.


; Not a complicated script but very useful 
;_______________________________________________________________________________ 
;  Usage: Follow the prompts and enter either a hexadecimal value, ie, an or 

many 
; integer/s or alphabetical value/s (Between A-F) proceeded by the prefix, 
; 0x (Zero Multiplied By). For example, 0x8 OR/ 0xFFACD. Or a ; ; decimal ;value 
; such as 987 or 250.  
;_______________________________________________________________________________ 
; 
; 

;--------------------------------------------- 
;HOTKEY- (Feel free to change to your liking) 
;--------------------------------------------- 

#p:: 

;--------------------------------------------- 
;HEX TO DECIMAL 
;--------------------------------------------- 

MsgBox 4, Hexadecimal Conversion Tool, Hexadecimal to Decimal? 

IfMsgBox, No 

Gosub, Label1 

IfMsgBox, Yes 

InputBox, Hex1, Enter a Hexadecimal value 

SetFormat, integer, d
Hex1 -= 0 

MsgBox, 0, Hexadecimal Conversion Tool, The Decimal value is %Hex1% 

Exit 

;--------------------------------------------- 
;DECIMAL TO HEX 
;--------------------------------------------- 

Label1: 

MsgBox, 4, Hexadecimal Conversion Tool, Decimal to Hexidecimal? 

IfMsgBox, No 

Exit 

InputBox, Dec1, Enter a Decimal value 

SetFormat, integer, h 
Dec1 += 0 

MsgBox, 0, Hexadecimal Conversion Tool, The Hexadecimal value is %Dec1%


Hope this helps.

!!Be sure to copy this script, not the one above!!

TomB
  • Members
  • 17 posts
  • Last active: Oct 29 2009 03:03 PM
  • Joined: 14 Jan 2005
I realize this is a long time since the last message in this thread, but I decided to update this script slightly. The two main differences are:

1- There is now no message box asking you which way you want to convert - it just gives you in inputbox where you enter a dec or hex value (in 0x format) and the script determines which type you entered and converts it to the other.

2- The converted value is copied to the clipboard so you can easily paste it into an application.

Tom Boughner

; Not a complicated script but very useful
;_______________________________________________________________________________
;  Usage: Enter either a decimal or hex value (must be preceded by 0x).  
;  The script will figure out which type you entered and convert it to the opposite.  
;  The converted value is displayed and placed on the clipboard.  
;_________________________________________________________________

;---------------------------------------------
;HOTKEY- (Feel free to change to your liking)
;---------------------------------------------

#h::

;---------------------------------------------
;Get Input
;---------------------------------------------

InputBox, inp, Hexadecimal Conversion Tool, Enter a decimal value or a hexadecimal value in '0x' format

StringLeft HorD, inp, 2    ;get left-most two characters
if (HorD = "0x")    ;Hex value entered
{
  SetFormat, integer, d
  inp -= 0
  clipboard = %inp%
  MsgBox, 0, Hexadecimal Conversion Tool, The Decimal value is %inp%
} 
else
{
  SetFormat, integer, h
  inp += 0
  clipboard = %inp%
  MsgBox, 0, Hexadecimal Conversion Tool, The Hexadecimal value is %inp%  
}

return


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
It could be useful. Btw, you can save a few lines with the newer AHK features
#h::

   InputBox inp, Hexadecimal Conversion Tool, Enter a decimal value or a hexadecimal value in '0x' format,,,170

   SetFormat Integer, % SubStr(inp,1,2) = "0x" ? "D" : "H"

   MsgBox 0, Hexadecimal Conversion, % "The converted value is " clipboard := inp+0

Return


TomB
  • Members
  • 17 posts
  • Last active: Oct 29 2009 03:03 PM
  • Joined: 14 Jan 2005
Wow - very cool. Can you optimize all of my code for me? :)

I am a novice programmer, so I haven't gotten too terribly advanced with AHK - but seeing your optimization opened my eyes to at least one area where I can save myself some keystrokes. Thanks Laszlo.

TomB

Riddick51PB
  • Members
  • 10 posts
  • Last active: Jul 16 2007 06:16 PM
  • Joined: 22 Apr 2007
windows hex calculator: Start, Run, calc [Enter], View, Scientific

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

windows hex calculator: Start, Run, calc [Enter], View, Scientific

You can assign these to a hotkey (otherwise it is many keystrokes or mouse clicks), but doing this way takes a couple of seconds, while the AHK solution is practically instantaneous. You can also easily adapt the AHK version to your needs, like 4-8-16 hex digits with padding, lower case or capital letters, etc.

  • Guests
  • Last active:
  • Joined: --
using hotkey to perform that would require excessive cpu and memory resources.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

using hotkey to perform that would require excessive cpu and memory resources.

It is not the case, if you have more hotkeys. Each one adds just a few bytes, and no measurable processor load.

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

windows hex calculator: Start, Run, calc [Enter], View, Scientific

You can assign these to a hotkey (otherwise it is many keystrokes or mouse clicks), but doing this way takes a couple of seconds, while the AHK solution is practically instantaneous. You can also easily adapt the AHK version to your needs, like 4-8-16 hex digits with padding, lower case or capital letters, etc.

I agree with you about the formatting customization (in particular, I think an automatic 0x prefix would be useful), however I find my own (calc!) method to be faster.

I have a calculator button (Launch_App2) on my keyboard, directly above the numpad. I'm much faster entering numbers with the numpad, so moving my hand from the mouse, to #h, then back to the numpad (or using the number keys) would be inefficient.

To convert to hex, I simply:
[*:2fm3zgie]Hit calc button.
[*:2fm3zgie]Type a decimal number.
[*:2fm3zgie]Hit calc button again. (If calc is active, it toggles hex/dec mode - thanks to AHK!)
[*:2fm3zgie]Ctrl+C to copy (though I don't always want to put it on the clipboard.)Can't get much faster than that...

Except with hex->dec conversion: I use Numpad0 as a modifier to enter hex digits with the numpad (1-6 become A-F.) :D

Reading this thread has given me an idea... Override ^c in calc to custom-format hexadecimal numbers. 8)