AutoHotkey Community

It is currently May 27th, 2012, 2:36 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: February 16th, 2010, 11:26 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
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
Image


Last edited by toralf on February 17th, 2010, 7:04 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 16th, 2010, 11:55 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
I found this in the one-liner code thread if that's what you mean.

EDIT: Laszlo shortened that code later on.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 17th, 2010, 7:04 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 18th, 2010, 9:51 am 
Offline

Joined: February 2nd, 2008, 4:35 am
Posts: 643
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 :?:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 18th, 2010, 11:01 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 18th, 2010, 12:02 pm 
Offline

Joined: February 2nd, 2008, 4:35 am
Posts: 643
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...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 3rd, 2010, 8:38 pm 
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!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2010, 4:03 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 8th, 2010, 6:52 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Thanks VxE

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Alpha Bravo, Google Feedfetcher, rbrtryn and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group