Sign in to follow this  
Followers 0
Welohabi

[SOLVED] Convert ASCII to HEX?

17 posts in this topic

#1 ·  Posted (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 by Welohabi

Share this post


Link to post
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)

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites

$result = Hex(1033, 4) ;returns "0409"

What problems are you having?

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#7 ·  Posted (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 by JohnQSmith

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

....

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 by Welohabi

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#16 ·  Posted (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 by Welohabi

Share this post


Link to post
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0