bobbby Posted June 9, 2011 Posted June 9, 2011 (edited) I have created a string to hex and hex to string converter. But how do I detect if it's hex and not a string? I tested with this but it fails for me sometimes. If i use a normal string. expandcollapse popup#include <string.au3> $var = string2hex("this is a simple test") $num = stringsplit($var,chr(32)) if isarray($num) and $num[0] > 0 then _run() func _run() for $i = 1 to $num[0] if stringregexp($num[$i],'[A-F0-9]{2}') == 1 Then else $i = $num[0] consolewrite("No Hex"&@crlf) EndIf Next consolewrite("No errors was found"&@crlf) EndFunc func hex2string($hex) local $str = stringsplit($hex,chr(32)) local $a if isarray($str) then if $str[0] > 0 then for $i = 1 to $str[0] $a = $a & _hextostring($str[$i]) Next return $a Else return 0 EndIf Else return 0 EndIf EndFunc func string2hex($string) local $b for $i = 1 to stringlen($string) if $i == 1 Then $b = $b & _StringToHex(stringmid($string,$i,1)) else $b = $b &" "&_StringToHex(stringmid($string,$i,1)) EndIf Next return $b EndFunc Edited June 9, 2011 by bobbby
BrewManNH Posted June 9, 2011 Posted June 9, 2011 I'm no regex expert, but deciding if a string is Hex by seeing if it contains A-F isn't going to work. There are plenty of Hex numbers that don't contain any of those characters. Take for instance "16" is that 22 in Hex, or 16 in base 10? If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
jchd Posted June 9, 2011 Posted June 9, 2011 The best you can do is determine is a string _may_ be hex (possibly adding the criterion that its length should be even) or not. Now, all this should use existing internal functions: Local $str = 'abcde' $hStr = Hex(StringToBinary(($str))) ConsoleWrite($hStr & @LF) $string = String(BinaryToString('0x' & $hStr)) ConsoleWrite($string & @LF) ConsoleWrite("$hStr is a possible hex string is " & (StringIsXDigit($hStr) = 1) & @LF) ConsoleWrite("$string is a possible hex string is " & (StringIsXDigit($string) = 1) & @LF) Local $test = 'grz hpo g42 jr574 h' ConsoleWrite("$test is a possible hex string is " & (StringIsXDigit($test) = 1) & @LF) 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 hereRegExp tutorial: enough to get startedPCRE 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)
GEOSoft Posted June 9, 2011 Posted June 9, 2011 Based on the suggestion (about checking to see if the length is an even number) from jchd and using RegEx this should put you on the right track but still may not be 100% accurate in all cases. Please remember that RegEx may in fact be slower than using the functions as given by jchd so avoid SREs if speed is important. $sStr = StringRegExpReplace($sStr, "^0x([[:xdigit:]]+)$", "$1") If (NOT Mod(StringLen($sStr), 2)) AND StringRegExp($sStr, "^[[:xdigit:]]+$") Then ConsoleWrite("Chances are good that the value is Hex" & @CRLF) EndIf Of course I just did a fast test of this and didn't try to implement it into your code. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
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