AutoHotkey Community

It is currently May 27th, 2012, 2:23 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: September 21st, 2005, 1:29 pm 
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.

Code:
; 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%


Report this post
Top
  
Reply with quote  
 Post subject: Hex/Dec Fix
PostPosted: September 22nd, 2005, 12:04 pm 
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.

Code:

; 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!!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 25th, 2007, 2:27 pm 
Offline

Joined: January 14th, 2005, 5:30 pm
Posts: 17
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

Code:
; 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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 25th, 2007, 4:42 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
It could be useful. Btw, you can save a few lines with the newer AHK features
Code:
#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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2007, 3:11 am 
Offline

Joined: January 14th, 2005, 5:30 pm
Posts: 17
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 28th, 2007, 4:38 pm 
Offline

Joined: April 22nd, 2007, 6:04 pm
Posts: 10
Location: Lincoln.ne.us
windows hex calculator: Start, Run, calc [Enter], View, Scientific


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 28th, 2007, 4:58 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Riddick51PB wrote:
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 28th, 2007, 8:43 pm 
using hotkey to perform that would require excessive cpu and memory resources.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 28th, 2007, 9:03 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Anonymous wrote:
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 30th, 2007, 5:16 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7503
Location: Australia
Laszlo wrote:
Riddick51PB wrote:
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:
  1. Hit calc button.
  2. Type a decimal number.
  3. Hit calc button again. (If calc is active, it toggles hex/dec mode - thanks to AHK!)
  4. 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)


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: MSN [Bot], Yahoo [Bot] and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group