r2du-soft Posted January 20 Share Posted January 20 (edited) I produce hardware serial numbers through the hard disk serial hardware. The problem is that the output of this serial is in hex code format in some systems. Now, I want to check if my code is in hex, convert it to a string. Another issue here is that sometimes the length of the generated code output varies! I have written the following code to solve the problem, but I want to know if there is a better way or not? #include <MsgBoxConstants.au3> #include <String.au3> Global $Hex_To_String ;$Hex_To_String = '2020202057202d44435736433659414550564b4a' ;$Hex_To_String = '2020202020202020202020205635434a43344434' $Hex_To_String = '343138334949313439373534200202020202020' MsgBox(0, "Example_1", Example_1()) Func Example_1() IF (StringIsXDigit($Hex_To_String)) _ ;Hex Check AND Not (StringInStr($Hex_To_String, " ")) _ ;Space AND Not (StringInStr($Hex_To_String, " ")) Then ; Space OR (alt+255) IF Mod(StringLen($Hex_To_String), 2) = 0 Then $Hex_To_String = _HexToString($Hex_To_String) ELSE $Hex_To_String = $Hex_To_String & "0" $Hex_To_String = _HexToString($Hex_To_String) ENDIF Else $Hex_To_String = $Hex_To_String ENDIF $Hex_To_String = StringStripCR($Hex_To_String) ;Remove NewLine $Hex_To_String = StringStripWS($Hex_To_String, $STR_STRIPALL) ;remove any other charanters! $Hex_To_String = StringReplace($Hex_To_String, " ", "") ;(alt+255) = (NBSP) Return $Hex_To_String EndFunc Edited January 20 by r2du-soft Link to comment Share on other sites More sharing options...
Nine Posted January 20 Share Posted January 20 (edited) There is a number of suggestions I could make about your code : 1- What if the string is NON-HEX but only contains hex chars [A-F0-9] ? It would then be considered as hex string ! 2- Number of characters should be the #1 criteria to determine if the string is hex or not. Larger than 20 (for example) is hex string. 3- Odd number of characters in hex string should return an @error 4- No need to check for space or 0xFF separately. StringIsXDigit will return an error if the string contains either one 5- No need to strip CR separately. StringStripWS strips CR and LF and other (see help file). Unless your serial numbers contains CR in the middle, which does not make much sense ! 6- No need to reassign $Hex_To_String = $Hex_To_String, it is useless 7- You should pass the input string as a parameter of the function, no need to use a global variable 8- Use Chr(255) instead of hardcoding it in quotes ps. in your example there is an odd number of characters, that causes the last characters to be 0x02 and not 0x20 (probably a typo from your part)... Edited January 20 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Solution Nine Posted January 20 Solution Share Posted January 20 Here my take on it : #include <Constants.au3> #include <String.au3> Local $Hex_To_String[] = [ _ '2020202057202d44435736433659414550564b4a' & @CRLF, _ Chr(255) & '2020202020202020202020205635434a43344434' & @CR, _ '123 DEAD 678 BEEF', _ '343138334949313439373534200202020202020' & Chr(32), _ "v5abcw76-mnbvvf"] Local $sResult For $i = 0 To UBound($Hex_To_String) - 1 $sResult = Example_1($Hex_To_String[$i]) If @error Then ContinueLoop MsgBox($MB_OK, "Invalid input on", $Hex_To_String[$i]) ConsoleWrite($sResult & @CRLF) ;MsgBox($MB_OK, "Example_1", $sResult) Next Func Example_1($sSerial) $sSerial = StringRegExpReplace($sSerial, "([\h\v\xFF])", "") ; remove all unwanted characters If StringLen($sSerial) > 20 And StringIsXDigit($sSerial) Then If Mod(StringLen($sSerial), 2) Then Return SetError(1) $sSerial = StringRegExpReplace(_HexToString($sSerial), "([\x20\xFF])", "") EndIf Return $sSerial EndFunc ;==>Example_1 r2du-soft 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now