Hex Conversions

Does anyone know how you would go about converting a hex string into a regular text string? And vice versa. I think you’d probably use a shell script, just not sure what it’d be. Thanks!

For shell scripting I use xxd, it’s standard Mac OS X and I use it since 10.3.x so it’s also not new.

stringToHex("Hello World!") -->68656c6c6f20776f726c6421
hexToString("476f6f642062796521") -->"Good bye!"

on stringToHex(__string)
	do shell script "/bin/echo -n " & quoted form of __string & " | xxd -p"
end stringToHex

on hexToString(__hex)
	do shell script "/bin/echo -n " & quoted form of __hex & " | xxd -r -p"
end hexToString

Thanks so much man!

What characters would I need backslashes in front of? Just the regular applescript ones? thanks

Also, if I convert an enter ("
") to hex I get 0d (like I should) but when I try to convert this back it goes away. So if I converted “A: Hi!
B: Yo!
C: blah” to hex then string I’d lose my enter and get “A: Hi!B: Yo!C: blah”. Do you know why this happens? And how to fix it? Thanks again, lol :smiley:
EDIT: Ok, here’s my code:

set theStartCode to "







"
repeat
	tell application "Finder"
		display dialog "Your code here:" default answer theStartCode with title "Code Translator" buttons {"Import from text file", "Box Above"} default button 2 -- IMPORT BETA**
		set theResult to result
		set thePrevCode to text returned of theResult
		set theButton to button returned of theResult
		set thePrevCode to my removeFrom(" ", thePrevCode, "")
		set thePrevCode to my removeFrom("
	", thePrevCode, "")
		set thePrevCode to my removeFrom("*", thePrevCode, "")
		set theStringCode to my hextostring(thePrevCode)
		display dialog "New Code:" default answer theStringCode with title "Code Translator" buttons {"Quit", "Convert Back"}
		set theResult to result
		if button returned of theResult is "Quit" then exit repeat
	end tell
	set theStartCode to my removeFrom("\"", text returned of theResult, "\\\"")
	--set theStartCode to my removeFrom("
	--", text returned of theResult, "\\r")
	set theStartCode to my stringToHex(theStartCode)
end repeat

on stringToHex(__string)
	do shell script "/bin/echo -n " & quoted form of __string & " | xxd -p"
end stringToHex

on hextostring(__hex)
	do shell script "/bin/echo -n " & quoted form of __hex & " | xxd -r -p"
end hextostring
on removeFrom(waht, fromWaht, replaceWith)
	repeat
		if (length of fromWaht) = 0 then
			return ""
			exit repeat
		else
			set TID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to waht
			set theWords to words in fromWaht
			set AppleScript's text item delimiters to replaceWith
			set theWords to theWords as string
			set AppleScript's text item delimiters to TID
			return theWords
		end if
	end repeat
end removeFrom

So if I use “413a204869210d0a423a20596f210d0a433a205761646475703f” as my input I get my example up there. But when I hit convert back I get the dud from up there (all one line)

you can enter any applescript string so yes only the double quotes and the backslash needs to be escaped.

How would i put an enter in then?
EDIT: Also, how do you go about putting quotes as applescript’s TID’s? Whenever I do:

Set applescript's text item delimiters to "\""

It doesnt give an error, but it does change the delimiters.

yes I know what happens. The stupid thing about do shell script (and default echo, that’s why I use /bin/echo -n command) is that it expects a return because almost any process prinst ou an extra new line at the end (so your next command can be typed on a new line). Therefore we not only need to force echo to skip the extra newline but also do shell script command. So my two functions/handlers shoudl be

on stringToHex(__string)
	do shell script "/bin/echo -n " & quoted form of __string & " | xxd -p"
end stringToHex

on hextostring(__hex)
	do shell script "/bin/echo -n" & quoted form of __hex & " | xxd -r -p" without altering line endings
end hextostring

Ok, that makes so much sense, thanks, cant wait to try it out! How about doing quotes in TID? I feel like im asking you too many questions… Lol, sorry, but thanks

Haha it’s ok to ask many questions.

first of al notice that I altered the code 2 minutes after the post…

The text item delimiters shouldn’t be a problem or maybe I don’t understand your question. The following scripts has an TID set to " without any problems so I think the problems lies elsewere.


set theString to "And the computer said \"Hello world!\""

set AppleScript's text item delimiters to "\""
set theTextItems to every text item of theString
set AppleScript's text item delimiters to ""
return theTextItems as string --result: "And the computer said Hello world!"

Yeah, I just figured out I don’t need to remove the quotes, i dont know why but it worked without it in there, so I’m good now, now I’m pretty much done, thanks for all the help!
PS: If you didn’t figure out the purpose of the code, it’s so you can edit a hex code without having to keep going back and forth between the two, it does all the conversions for you :smiley:

I thought it was something like that. Well the next step is a hex editor with a split view :stuck_out_tongue:

also a minor comment: why is there a repeat in the removeFrom handler? it never loops so it doesn’t need to be in there in the first place and the exit repeat can be removed too.

Yeah, I dont need that, I was gonna use it but found a better way and never took it out lol. Ok, final question: I just got an error when I entered “000004F8” and then converted it back. Why is this/How do I fix it?
EDIT: It’s whenever I try and convert a “~” (squiggle) to hex, im pretty sure its cause shell script’s use it a lot, so how would i make it read it as part of the string, and not a shell command?
EDIT(2): Ok, ACTUALLY it’s whenever I have "00"s in my hex. Same questions, why/solution :slight_smile: lol

The error you’ll get is strange because it starts with double zero’s which means byte zero. A zero byte can’t be typed from the keyboard and that makes it strange unless you created the 000004F8 with the convert back option. Then I get no error on my machine only the zero bytes are ignored like it supposed to do in lower level your machine. Then the squigle should be 7e (sopied pasted from macscripter) which gives me no problem too. I’m not sure why you are coercing any characters try this script… will do the same as yours…


set currentCode to string id {10, 10, 10, 10, 10, 10, 10} as string

repeat
	set theResult to (display dialog "Convert String to HEX" default answer currentCode buttons {"Continue", "Quit"})
	if button returned of theResult = "Quit" then
		return
	end if
	set currentCode to stringToHex(text returned of theResult)
	set theResult to (display dialog "Convert Hex to String" default answer currentCode buttons {"Continue", "Quit"})
	if button returned of theResult = "Quit" then
		return
	end if
	set currentCode to hextostring(text returned of theResult)
end repeat

on stringToHex(__string)
	do shell script "/bin/echo -n " & quoted form of __string & " | xxd -p"
end stringToHex

on hextostring(__hex)
	do shell script "/bin/echo -n" & quoted form of __hex & " | xxd -r -p" without altering line endings
end hextostring

It’s because the thing im copying the hexes to convert from always has *‘s, space, and enters and I dont want them there. And I have the “00” thing figured out (not a solution). It’s if I have 00 in and I replace it with “” (supposed to be nothing) it gives me an EOF error. Apparently its putting some invisible character there instead of just “”. If I hit cmd + a and then delete, it works, but if I just add to the string it gives me, it gives an error. You get what I’m saying?
Here’s the error:
AppleScript Error
sh: -c: line 0: unexpected EOF while looking for matching `’’
sh: -c: line 1: syntax error: unexpected end of file
Here’s my current code:

set theStartCode to "







"
repeat
	tell application "Finder"
		display dialog "Your code here:" default answer theStartCode with title "Code Translator" buttons {"Import from text file", "Box Above"} default button 2 -- IMPORT BETA**
		set theResult to result
		set thePrevCode to text returned of theResult
		set theButton to button returned of theResult
		set thePrevCode to my removeFrom("00", thePrevCode, "")
		set thePrevCode to my removeFrom(" ", thePrevCode, "")
		set thePrevCode to my removeFrom("
	", thePrevCode, "")
		set thePrevCode to my removeFrom("*", thePrevCode, "")
		set theStringCode to my hextostring(thePrevCode)
		display dialog "New Code:" default answer theStringCode with title "Code Translator" buttons {"Quit", "Convert Back"}
		set theResult to result
		if button returned of theResult is "Quit" then exit repeat
	end tell
	set theStartCode to (text returned of theResult)
	if not theStartCode = "" then
		set theStartCode to my stringtohex(theStartCode)
	else
		set theStartCode to "
	
	
	
	
	"
	end if
end repeat
on stringtohex(__string)
	do shell script "/bin/echo -n " & (quoted form of __string) & " | xxd -p"
end stringtohex

on hextostring(__hex)
	do shell script "/bin/echo -n" & quoted form of __hex & " | xxd -r -p" without altering line endings
end hextostring
on removeFrom(waht, fromWaht, replaceWith)
	if (length of fromWaht) = 0 then
		return ""
	else
		set TID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to waht
		set theWords to words in fromWaht
		set AppleScript's text item delimiters to replaceWith
		set theWords to theWords as string
		set AppleScript's text item delimiters to TID
		return theWords
	end if
end removeFrom

So I guess if I used your code, but then coerced the list (using some other method, if u have an idea post it) it would work

EDIT: Nope, your code didnt make a difference. I type in “220d0a2022000000” which converts correctly to:
"
_"
(underscore is a space, it didnt show up right when i posted it so i used that)
but if I try to go back after that I get an EOF error again:
sh: -c: line 0: unexpected EOF while looking for matching `‘’
sh: -c: line 2: syntax error: unexpected end of file
same one… You know why?

Ok, got my entire code working, I just need a solution to the “00” thing if you have one, is there anything that’ll remove this invisible character, like putting it in a textedit doc then pulling it back out? thanks!

A byte 0 is a Null character. Xxd is probably written in C and C uses Null terminated strings. So in your case the EOF doesn’t match the string length so it throws an error.

how does adding the line


set thePrevCode to my removeFrom(string id 0, thePrevCode, "")

sounds like?

I fixed my removeFrom at the end and it all works now, I don’t know how that connects to it but I’m happy, i’ll take it, lol. Thanks though!

Well it’s quite simple. You have to remember that almost any utility in UNIX is written in C that are part of POSIX. C uses null terminated strings which means the string’s content is from the pointer to the first null character. A well written command line utility would check if the length of the string is equal to the null character position for security reasons (it avoids buffer overflows). You have a 6 character long string but the null character is not at the last position, the application must throw an error. So simply escape the null character or remove it and it’ll work. As long as you’re working with strings instead of binary data I don’t see the problem of removing characters 0 thru 7 and character 17 - 20 (are also very very old computer instructions which some applications still responds to).

Yeah, still a little confusing but I think I get what your saying, thanks :slight_smile: