Sign in to follow this  
Followers 0
kaesereibe

int2hex

14 posts in this topic

#1 ·  Posted (edited)

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 this post


Link to post
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.
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)

Share this post


Link to post
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.
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)

Share this post


Link to post
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.
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)

Share this post


Link to post
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._______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 this post


Link to post
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.
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)

Share this post


Link to post
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 this post


Link to post
Share on other sites

#13 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Nice gists Mat.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

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