Welohabi Posted August 29, 2012 Share Posted August 29, 2012 (edited) I have searched for a few days now and found several post very close to being able to convert ASCII to HEX but I am still unable to do it. So I am here asking for help.I want the user to be able to enter some ASCII into an edit box on my GUI and then click a button to convert it to HEX. Thanks for all help. Edited August 30, 2012 by Welohabi Link to comment Share on other sites More sharing options...
hannes08 Posted August 29, 2012 Share Posted August 29, 2012 Just a little example: $sIn = "This is a Text!" $aCharacters = StringSplit($sIn, "") For $i = 1 To $aCharacters[0] ConsoleWrite(Asc($aCharacters[$i]) & " ") Next ConsoleWrite(@LF) Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler] Link to comment Share on other sites More sharing options...
Tripredacus Posted August 29, 2012 Share Posted August 29, 2012 $result = Hex(1033, 4) ;returns "0409" What problems are you having? Twitter | MSFN | VGCollect Link to comment Share on other sites More sharing options...
Welohabi Posted August 29, 2012 Author Share Posted August 29, 2012 Just a little example: $sIn = "This is a Text!" $aCharacters = StringSplit($sIn, "") For $i = 1 To $aCharacters[0] ConsoleWrite(Asc($aCharacters[$i]) & " ") Next ConsoleWrite(@LF) I don't even slightly understand this.. But i did find this... $Ascii2Hex = AscToHex("1234567812345678") MsgBox(0,"The Output",$Ascii2Hex) Func AscToHex($strString) ;ascii to hex Local $ah="" For $i = 1 To StringLen($strString) If StringLen(Hex(Asc(StringMid($strString, $i, 1)),2)) = 1 Then $ah &= "0" & Hex(Asc(StringMid($strString, $i, 1))) Else $ah &= Hex(Asc(StringMid($strString, $i, 1)),2) EndIf If $i <> StringLen($strString) Then $ah &= " " Next Return $ah endFunc And it basically does what I want it to do. I just need to format the output correctly. Currently the output looks like this 31 32 33 34 35 36 37 38 31 32 33 34 35 36 37 38 But i want it too look exactly like this: 31323334 35363738 31323334 35363738 Link to comment Share on other sites More sharing options...
Belini Posted August 29, 2012 Share Posted August 29, 2012 I use a program that gives me the codes: CHAR, DEC, HEX, OCT, BIN and Virtual Keycodes, I hope to help you. Link: http://www.mediafire.com/?1z1as1u6dufe2o6 My Codes: Â Virtual Key Code UDF: http://www.autoitscript.com/forum/topic/138246-virtual-key-code-udf/ GuiSplashTextOn.au3: http://www.autoitscript.com/forum/topic/143542-guisplashtexton-udf/ Menu versions of Autoit: http://www.autoitscript.com/forum/topic/137435-menu-versions-of-autoit/#entry962011 Selects first folder of letters: ]http://www.autoitscript.com/forum/topic/144780-select-folders-by-letter/#entry1021708/spoiler] List files and folders with long addresses.: http://www.autoitscript.com/forum/topic/144910-list-files-and-folders-with-long-addresses/#entry102 Â 2926 Program JUKEBOX made in Autoit:some functions:http://www.youtube.com/watch?v=WJ2tC2fD5Qs Navigation to search:http://www.youtube.com/watch?v=lblwOFIbgtQ Â Â Link to comment Share on other sites More sharing options...
dany Posted August 29, 2012 Share Posted August 29, 2012 Just use StringToASCIIArray() on the string and then loop through the resulting array and convert every ASCII code to Hex(). After that you can use StringFormat() to get the output formatting you want. [center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF Link to comment Share on other sites More sharing options...
JohnQSmith Posted August 29, 2012 Share Posted August 29, 2012 (edited) I don't even slightly understand this.. $sIn = "This is a Text!" ;<--- Assigns some text to a string variable $aCharacters = StringSplit($sIn, "") ;<--- Breaks the input string into an array ; of characters For $i = 1 To $aCharacters[0] ;<--- Loops through the array one character ; at a time ConsoleWrite(Asc($aCharacters[$i]) & " ") ;<--- Write the ASCII value of each ; character to the console Next; ;<--- Next loop iteration ConsoleWrite(@LF); ;<--- Write a line feed to the console Is that any better? Edited August 29, 2012 by JohnQSmith Welohabi 1 Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes". Link to comment Share on other sites More sharing options...
Blue_Drache Posted August 29, 2012 Share Posted August 29, 2012 I don't even slightly understand this..ROFL. I see someone missed programming 101. JQSmith explained it perfectly, however, I'd like to point out that manipulating the output string from the original function given would be a snore ... and something to let you learn how to code your own stuff. Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
Welohabi Posted August 29, 2012 Author Share Posted August 29, 2012 Is that any better?Yes this helps me a lot. Thank you.ROFL. I see someone missed programming 101. JQSmith explained it perfectly, however, I'd like to point out that manipulating the output string from the original function given would be a snore ... and something to let you learn how to code your own stuff.I have never taken any programming classes, nor any college classes for that matter. Could you explain what you mean about the output string? Are you referring to the code snippet I posted or the one JohnQSmith posted? Link to comment Share on other sites More sharing options...
Solution Malkey Posted August 30, 2012 Solution Share Posted August 30, 2012 .... But i want it too look exactly like this: 31323334 35363738 31323334 35363738 Here are two methods achieving what you desire. #include <String.au3> Local $String = "1234567812345678" Local $Hex = _StringToHex($String) $Hex = StringRegExpReplace($Hex, "(.{0,8})", "1 ") ; Insert a space after ever 8th character. $Hex = StringRegExpReplace($Hex, "(.{0,17}) ", "1" & @LF) ; Replace every 18th character with a linefeed, @LF. MsgBox(0, "Hex", "Original String: " & $String & @LF & " Hex: " & @LF & $Hex) ; Or ; --------------------------------------------------------------- Local $Ascii2Hex = AscToHex("1234567812345678") MsgBox(0, "The Output", $Ascii2Hex) Func AscToHex($strString) Local $ah = "" For $i = 1 To StringLen($strString) $ah &= Hex(Asc(StringMid($strString, $i, 1)), 2) If Mod($i, 8) = 0 Then $ah &= @LF ; Insert a line feed, @LF, after every 8th character. ElseIf Mod($i, 4) = 0 Then $ah &= " " ; Insert a space after ever 4th character EndIf Next Return $ah EndFunc ;==>AscToHex Welohabi 1 Link to comment Share on other sites More sharing options...
water Posted August 30, 2012 Share Posted August 30, 2012 (edited) Or maybe this helps. It shows a string as character, hex and decimal.Output of the test script looks like: Edited August 30, 2012 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki  Link to comment Share on other sites More sharing options...
Welohabi Posted August 30, 2012 Author Share Posted August 30, 2012 (edited) Here are two methods achieving what you desire. #include <String.au3> Local $String = "1234567812345678" Local $Hex = _StringToHex($String) $Hex = StringRegExpReplace($Hex, "(.{0,8})", "1 ") ; Insert a space after ever 8th character. $Hex = StringRegExpReplace($Hex, "(.{0,17}) ", "1" & @LF) ; Replace every 18th character with a linefeed, @LF. MsgBox(0, "Hex", "Original String: " & $String & @LF & " Hex: " & @LF & $Hex) This is exactly what I needed. Thank you Malkey, and everyone else also. I have one more question. I am using a text box to limit the input to 16 characters. If the user enters less than 16 characters I want the remainder of the possible characters to show up as zeros or spaces (in HEX). Here is an example: #include <GuiConstantsEx.au3> #include <String.au3> $MainGUI = GUICreate("Example", 500, 300) $classname = GUICtrlCreateInput("This is Text", 350, 5, 85, 20) GUICtrlSetLimit(-1, 16) $THEBOX = GUICtrlCreateEdit("", 2, 2, 333, 270) $convert = GUICtrlCreateButton("CONVERT", 350, 55, 75, 18) GUISetState(@SW_SHOW) While 1 $guimsg = GUIGetMsg() Select Case $guimsg = $convert $String = GUICtrlRead($classname) $Hex = _StringToHex($String) $Hex = StringRegExpReplace($Hex, "(.{0,8})", "1 ") $Hex = StringRegExpReplace($Hex, "(.{0,17}) ", "1" & @CRLF) GUICtrlSetData($THEBOX, $Hex) EndSelect Select Case $guimsg = $GUI_EVENT_CLOSE Exit EndSelect WEnd If you convert "This is Text" the output is: 54686973 20697320 54657874 I want the output to be: 54686973 20697320 54657874 00000000 or: 54686973 20697320 54657874 20202020 Thanks for all of the help. Edited August 30, 2012 by Welohabi Link to comment Share on other sites More sharing options...
trancexx Posted August 30, 2012 Share Posted August 30, 2012 (edited) Then you do something like this: $sIn = "This is Text" $sOut = StringRegExpReplace(Hex(Binary($sIn & StringLeft(" ", BitAND(16 - Mod(StringLen($sIn), 16), 15)))), "(.{8})(.{8})", "$1 $2" & @CRLF) MsgBox(0,"", $sOut) Edited August 30, 2012 by trancexx Welohabi 1 ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Welohabi Posted August 30, 2012 Author Share Posted August 30, 2012 Then you do something like this: $sIn = "This is Text" $sOut = StringRegExpReplace(Hex(Binary($sIn & StringLeft(" ", BitAND(16 - Mod(StringLen($sIn), 16), 15)))), "(.{8})(.{8})", "$1 $2" & @CRLF) MsgBox(0,"", $sOut) Thank You! This is excellent, I'm going to research all these commands your using because this looks simple. How do I get rid of the final @CRLF line break? Link to comment Share on other sites More sharing options...
Blue_Drache Posted August 30, 2012 Share Posted August 30, 2012 How do I get rid of the final @CRLF line break?Remove it from the spoon-fed code that was given to you. Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache Link to comment Share on other sites More sharing options...
Welohabi Posted August 30, 2012 Author Share Posted August 30, 2012 (edited) Remove it from the spoon-fed code that was given to you.Removing the @CRLF from the code removes all the line breaks not just the final one. Read the thread to understand the format I was trying to get.EDIT: I solved my own problem.A big thanks to everyone who helped!If anyone could explain the snippet trancexx gave me in greater detail I would be grateful because regular expression confuses me and I would rather learn from this experience and not just copy&paste. (Not that I'm not already grateful) Edited August 31, 2012 by Welohabi Link to comment Share on other sites More sharing options...
dany Posted August 30, 2012 Share Posted August 30, 2012 This is a great site for regular expressions: http://www.regular-expressions.info/() = a group, anything captured can be backreferenced in the replace string . = any character, but not newline or empty string '' {n} = quantifier (or repeating modifier) (.{8})(.{8}) = get any 8 characters and make them available in $0, then repeat for $1 Welohabi 1 [center]Spiderskank Spiderskank[/center]GetOpt Parse command line options UDF | AU3Text Program internationalization UDF | Identicon visual hash UDF 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