Jump to content

[SOLVED] Convert ASCII to HEX?


Go to solution Solved by Malkey,

Recommended Posts

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

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

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

Link to comment
Share on other sites

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

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 by JohnQSmith

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

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

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

  • Solution

....

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

Or maybe this helps. It shows a string as character, hex and decimal.

Output of the test script looks like:

post-7903-0-73339700-1346323123_thumb.pn

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.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 (NEW 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

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

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

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

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

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

[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

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...