# int2hex

## Recommended Posts

yet another int2hex() funtion

This example shows just a way to calculate int to hex manually.

Otherwise please use the Hex() function.

```; AutoIt v3.3.12.0
#include <MsgBoxConstants.au3>

MsgBox(\$MB_OK, "int2hex", int2hex(119) & " | " & int2hex(2147483647) & " | " & int2hex(4294967295))

Func int2hex(\$int)
Local \$h
\$BIT_BYTE = 8                                                     ; bits per byte
\$HEX_BYTE = \$BIT_BYTE / 4                                         ; hex digits in a byte
\$BIT_INT  = 32                                                    ; sizeof(int)
\$HEX_INT  = \$BIT_INT / (\$BIT_BYTE / \$HEX_BYTE)                    ; hex digits in an int
While \$HEX_INT                                                    ; while HEX_INT--
\$HEX_INT -= 1                                                 ; while HEX_INT--
\$n = BitAnd(BitShift(\$int, (\$HEX_INT * 4)), 0xf)              ; (int >> (HEX_INT * 4)) & 0xf
\$h &= \$n > 9 ? chr(0x37 + \$n) : \$n
If \$HEX_INT == 0 AND Floor(\$HEX_INT / \$HEX_BYTE) == 0 Then    ; if (HEX_INT == 0 && HEX_INT // HEX_BYTES == 0)
\$h &= " "
EndIf
WEnd
Return "0x" & \$h
EndFunc    ; ==> int2hex()```
Edited by kaesereibe

##### Share on other sites

Who need this (wrong) example to compete with the built-in Hex()?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
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)

##### Share on other sites

my goal was to calc this manual an not to use Hex()
explain why its wrong

Edited by jNizM

##### Share on other sites

This code won't handle int64

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
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)

##### Share on other sites

as you can read its for 32

`\$BIT_INT = 32 ; sizeof(int)`
Edited by jNizM

##### Share on other sites

That means it's inferior to built-in Hex() function. So why bother creating it in the first place?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
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)

##### Share on other sites

Be a nice guy and do me a personal favor. Ignore my Topic pls. Because it's useless to discuss with you!!!

It shows just another way without hex()

thx bye bye

Edited by jNizM

##### Share on other sites

jNizM,

Keep calm please. This is a semi-public forum - jchd has as much right to comment on your code as you have to post it.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

jNizM,

I'm not on a personal war against you or your posts. Just that I question the usefulness of code inferior to what comes standard with AutoIt. It's merely going to fool beginners.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
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)

##### Share on other sites

Changed first post

(it was the way how you posted)

Edited by kaesereibe

##### Share on other sites

Here's generic base conversion functions that I've been using for years: https://gist.github.com/MattDiesel/5813398

##### Share on other sites

same here:

```#include <MsgBoxConstants.au3>

Func _TranslateBase(\$sNumber, \$iOldBase = 10, \$iNewBase = 2) ; _TranslateBase() by eukalyptus
If \$iOldBase < 2 Or \$iOldBase > 128 Or \$iNewBase < 2 Or \$iNewBase > 128 Then Return SetError(1, 1, False)
Local \$iNum, \$aRes, \$tChr = DllStructCreate("char[64];")
If \$iOldBase <> 10 Then
\$aRes = DllCall("msvcrt.dll", "uint64:cdecl", "_strtoui64", "str", \$sNumber, "ptr", 0, "int", \$iOldBase)
If @error Then Return SetError(1, 2, False)
\$iNum = \$aRes[0]
Else
\$iNum = Int(\$sNumber)
EndIf
\$aRes = DllCall("msvcrt.dll", "ptr:cdecl", "_i64toa", "int64", \$iNum, "ptr", DllStructGetPtr(\$tChr), "int", \$iNewBase)
If @error Then Return SetError(1, 3, False)
Return DllStructGetData(\$tChr, 1)
EndFunc   ;==>_TranslateBase

\$decToBin = _TranslateBase(111, 10, 2)
\$binToDec = _TranslateBase(01101111, 2, 10)

MsgBox(4096, "TranslateBase", "Decimal to Binary:" & @CRLF & "111 to " & \$decToBin)
MsgBox(4096, "TranslateBase", "Binary to Decimal:" & @CRLF & "01101111 to " & \$binToDec)```

##### Share on other sites

It's good to try out things for yourself and learn through this experience. I consider that a valid reason to post such an example, even if there are more flexible versions available. By this process it may be possible to learn about design flaws and optimization proceedures. While I have received criticism for several of my own posts, it has mainly been to my benefit in the long run. I don't always listen to everyone, but I can assure you that MVPs such as Mat and JCHD don't normally comment unless they feel there is justification.

There is one thing I am wondering about: I'm not sure why you added this. ==>

```If \$HEX_INT == 0 AND Floor(\$HEX_INT / \$HEX_BYTE) == 0 Then    ; if (HEX_INT == 0 && HEX_INT // HEX_BYTES == 0)
\$h &= " "
EndIf```

;

I urge you to continue trying out your own ideas, providing you also research tried and tested methods; and I wish you good luck with your code projects.

Edited by czardas

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...