AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Solved] Convert Ascii String to Hex

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Tue Feb 16, 2010 10:26 pm    Post subject: [Solved] Convert Ascii String to Hex Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
sinkfaze



Joined: 18 Mar 2008
Posts: 5043
Location: the tunnel(?=light)

PostPosted: Tue Feb 16, 2010 10:55 pm    Post subject: Reply with quote

I found this in the one-liner code thread if that's what you mean.

EDIT: Laszlo shortened that code later on.
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Wed Feb 17, 2010 6:04 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
a_h_k



Joined: 02 Feb 2008
Posts: 626

PostPosted: Thu Feb 18, 2010 8:51 am    Post subject: Reply with quote

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 Question
Back to top
View user's profile Send private message Visit poster's website
toralf-n-l-i
Guest





PostPosted: Thu Feb 18, 2010 10:01 am    Post subject: Reply with quote

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

PostPosted: Thu Feb 18, 2010 11:02 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
hex_2_ascii
Guest





PostPosted: Wed Mar 03, 2010 7:38 pm    Post subject: Reply with quote

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

PostPosted: Thu Mar 04, 2010 3:03 am    Post subject: Reply with quote

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 Confused
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
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Mon Mar 08, 2010 5:52 pm    Post subject: Reply with quote

Thanks VxE
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group