Jump to content

The best way to convert hex code to a string


Go to solution Solved by Nine,

Recommended Posts

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 by r2du-soft
Link to comment
Share on other sites

  • r2du-soft changed the title to The best way to convert hex code to a string

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 by Nine
Link to comment
Share on other sites

  • Solution

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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...