 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
toralf
Joined: 31 Jan 2005 Posts: 3910 Location: Bremen, Germany
|
Posted: Tue Feb 16, 2010 10:26 pm Post subject: [Solved] Convert Ascii String to Hex |
|
|
Hi,
I might have missed this on the forum, but I couldn't find it with the search function.
I want to convert ASCII to HEX. This is what I came up with. Is there a more elegant way? I feel a bit stupid, because I assume I made it more complicated then needed. Any ideas or hints?
| Code: | ASCII2Hex(StringA){
Loop, Parse, StringA
StringH .= base(Asc(A_LoopField),10,16)
Return "0x" . StringH
}
;www.autohotkey.com/forum/viewtopic.php?p=159863#159863
;by haichen
base(num,inputbase,outputbase){
VarSetCapacity(S,65,0)
toDec := DllCall("msvcrt\_strtoui64", Str,num, Uint,0, Int,inputbase, "CDECL Int64")
DllCall("msvcrt\_i64toa", Int64,toDec, Str,S, Int,outputbase)
return, S
} |
_________________ Ciao
toralf 
Last edited by toralf on Wed Feb 17, 2010 6:04 pm; edited 1 time in total |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3910 Location: Bremen, Germany
|
Posted: Wed Feb 17, 2010 6:04 pm Post subject: |
|
|
Thanks a lot. Very much appreciated.
And that's why I felt stupid. I knew that there is somewhere an ellegant solution. Thanks again for the help. _________________ Ciao
toralf  |
|
| Back to top |
|
 |
a_h_k
Joined: 02 Feb 2008 Posts: 626
|
Posted: Thu Feb 18, 2010 8:51 am Post subject: |
|
|
Here's a way using SetFormat...
| Code: | Ascii_2_Hex(StringT)
{
SetFormat, IntegerFast, H
Loop, Parse, StringT
{
CharH := Asc(A_LoopField)
StringTrimLeft, CharH, CharH, 2 ;rid "0x" part
StringH .= CharH
}
;StringH := "0x" . StringH
return StringH
}
|
Was this the simple solution you were originally looking for  |
|
| Back to top |
|
 |
toralf-n-l-i Guest
|
Posted: Thu Feb 18, 2010 10:01 am Post subject: |
|
|
Dear a_h_k
Thanks for the reply. But no, I wasn't looking for that solution, since it messes with the Format. I was expecting that there is somewhere on this forum a one-liner or DllCall to do it. |
|
| Back to top |
|
 |
a_h_k
Joined: 02 Feb 2008 Posts: 626
|
Posted: Thu Feb 18, 2010 11:02 am Post subject: |
|
|
You probably also know that you could always store-->restore the current format via A_FormatFloat & A_FormatInteger ?
(I use my own functions SetFormat(), StoreFormat(), & RestoreFormat() to automatically handle this)
| Code: | Ascii_2_Hex(StringT)
{
SetFormat("IntegerFast", "H")
Loop, Parse, StringT
{
CharH := Asc(A_LoopField)
StringTrimLeft, CharH, CharH, 2 ;rid "0x" part
StringH .= CharH
}
;StringH := "0x" . StringH
RestoreFormat()
return StringH
} |
But this is obviously straying even further away from the 1-liner you wanted... |
|
| Back to top |
|
 |
hex_2_ascii Guest
|
Posted: Wed Mar 03, 2010 7:38 pm Post subject: |
|
|
How about hex to ASCII?
I've found this:
| Ian_School wrote: | | Code: | encrypt(sData) {
Loop, Parse, sData
r .= Chr(Asc(A_LoopField)+3)
Return r
}
decrypt(sData) {
Loop, Parse, sData
r .= Chr(Asc(A_LoopField)-3)
return r
}
|
|
But I cant use it properly
Is that even the best way to bring hex to ascii?
Examples please! |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
Posted: Thu Mar 04, 2010 3:03 am Post subject: |
|
|
| hex_2_ascii wrote: | How about hex to ASCII?
I've found this:
| Ian_School wrote: | | Code: | encrypt(sData) {
Loop, Parse, sData
r .= Chr(Asc(A_LoopField)+3)
Return r
}
decrypt(sData) {
Loop, Parse, sData
r .= Chr(Asc(A_LoopField)-3)
return r
}
|
|
But I cant use it properly
Is that even the best way to bring hex to ascii?
Examples please! |
That code is not a hex-to-ascii converter, not even close.
This is a function that converts a hex string into ascii, but it is not the best (there are several ways to accomplish this) | Code: | Hex_ToAscii( hex ) {
VarSetCapacity( string, StrLen( hex ) // 2, 0 )
Loop, Parse, hex
If ( A_Index & 1 ) ; if this is an ODD-numbered loop iteration
nibble := InStr( "123456789abcdef", A_LoopField )
Else If ( A_LoopField != "x" )
string .= Chr( InStr( "123456789abcdef", A_LoopField ) | nibble << 4 )
Return string
}
MsgBox % Hex_ToAscii( "0x48656c6c6f20576f726c6421" ) |
_________________ Ternary (a ? b : c) guide TSV Table Manipulation Library
Post code inside [code][/code] tags! |
|
| Back to top |
|
 |
toralf
Joined: 31 Jan 2005 Posts: 3910 Location: Bremen, Germany
|
Posted: Mon Mar 08, 2010 5:52 pm Post subject: |
|
|
Thanks VxE _________________ Ciao
toralf  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|