• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Routines for review: Hex <-> String
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Routines for review: Hex <-> String


  • Subject: Re: Routines for review: Hex <-> String
  • From: Philip Aker <email@hidden>
  • Date: Sun, 19 Jan 2003 10:26:46 -0800

On Sunday, January 19, 2003, at 08:49 AM, Paul Skinner wrote:

StringToHex still has limits. It can't deal with >32k of text. Somewhere in the execution of the do shell script. So if you need to hex your logs you're going to have to tweek it.

Way cool guys. Here's my $0.02:

set str to "Best of all possible worlds."
on str2hex(theString)
do shell script "echo -n " & theString & " | hexdump -v -e '1/1 \"x\"'"
end str2hex
str2hex(str)

Wicked fast OMM.


Philip

On Sunday, January 19, 2003, at 08:49 AM, Paul Skinner wrote:

On Saturday, January 18, 2003, at 06:08 AM, julifos wrote:

snip

I like this.

set hex to stringtoHex("hello world")
-->{"68", "65", "6c", "6c", "6f", "20", "77", "6f", "72", "6c", "64"}
set AppleScript's text item delimiters to "%"
HexToString("%" & hex)
-->"hello world"
set AppleScript's text item delimiters to ""


on stringtoHex(s)
words 2 thru ((length of s) + 1) of (do shell script "echo " & s & " |
hexdump -C")
end stringtoHex

Very short code! But this routine is very slow and it won't handle large
strings, unless you say:

text from word 2 of x to word y of x

(quick note off-topic: I've seen that
text from character 1 of x to character 5 of x
If much quicker than
characters 1 thru 5 of x as string
)


interesting. I'll test.

on HexToString(h)
do shell script "echo " & h & " | perl -ple
's/%([0-9a-fA-F]{2})/chr(hex($1))/ge' "
end HexToString

Hmmm... This doesn't work for me... Do I need install something? It returns:
--> "h656c6c6f20776f726c64"

JJ

Did you call it like so...

set AppleScript's text item delimiters to "%"
HexToString("%" & hex)

and see below.

And On Sunday, January 19, 2003, at 07:03 AM, Axel Luttgens wrote:

Paul Skinner wrote:

Has kindly pointed out, off list, that my stringToHex fails with more than 16 characters. Doh!
Sooo...

[snip]

: ( there goes my one-liner.


From another thread on this list:

on HexString2(AString)
text 1 through -3 of (do shell script
"echo '" & AString & "' | hexdump -e '1/1 \"X\"'")
end HexString2

The one-liner way is not gone :-)

I wondered if there could be a better choice for the arguments of hexdump.

Axel

Nice. It also needs a -v or it'll swap runs of characters into asterisks and slashes. I couldn't (still can't if the OP of this code is known.) follow the formatting instructions in the man page for fprintf. Documentation is nice, wish I could comprehend the meaning.

So...

set txt to "Best of all possible worlds."
set hex to StringToHex(txt)
set str to HexToString(hex)

on StringToHex(str)
text 1 through -3 of (do shell script "echo '" & str & "' | hexdump -v -e '1/1 \"X\"'")
end StringToHex

on HexToString(hex)
do shell script "echo " & hex & " | perl -ple's/([0-9a-fA-F]{2})/chr(hex($1))/ge' "
end HexToString

StringToHex still has limits. It can't deal with >32k of text. Somewhere in the execution of the do shell script. So if you need to hex your logs you're going to have to tweek it.


Philip Aker
http://www.aker.ca
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Routines for review: Hex <-> String
      • From: Axel Luttgens <email@hidden>
References: 
 >Re: Routines for review: Hex <-> String (From: Paul Skinner <email@hidden>)

  • Prev by Date: Re: Routines for review: Hex <-> String
  • Next by Date: re: UI scripting, windows order
  • Previous by thread: Re: Routines for review: Hex <-> String
  • Next by thread: Re: Routines for review: Hex <-> String
  • Index(es):
    • Date
    • Thread