Whoops, never mind.  I figured it out moments after sending the last email...

        set oStr to n as text
        return oStr

Here is the complete script, which works for 0x#### and 0X####...

-- File: Convert_Hex2Dec.scpt
tell application "BBEdit"
        set top to true
        repeat
set f to find "(0[xX])([0-9a-fA-F]+)" searching in text 1 of text document 1 options {search mode:grep, starting at top:top, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
                if (found of f) then
                        set t to grep substitution of "\\2"
                else
                        exit repeat
                end if
                set selection of window 1 to my fromHex(t)
                set top to false
        end repeat
end tell

on fromHex(str)
        set n to 0
        set i to 1
        repeat with a in reverse of every text item of str
                if a is greater than 9 or a is less than 0 then
                        set a to ASCII number of a
                        if a is greater than 70 then
                                set a to a - 87
                        else
                                set a to a - 55
                        end if
                end if
                set n to n + (a * i)
                set i to (i * 16)
        end repeat
        set oStr to n as text
        return oStr
end fromHex

Thanks to all for their help and suggestions (including the ones regarding perl :)).

Ken P.



At 11:03 AM -0700 6/2/06, Kenneth Prager wrote:
Okay, thanks.  Last hurdle...

On the return from fromHex(), I get the error: "...Could not make the data into the desired type..."

It appears that I need to convert the number n back into a string. I've tried several things including...

        set oStr to string of n
        return oStr

(which returns the error: Can't get string of <n>)

and

        set oStr to text of n
        return oStr

(which returns the error: Can't get every text of <n>)

Thanks,

Ken P.



At 9:19 AM -0400 6/2/06, David Livesay wrote:
You would call it from an AppleScript. That script would look something like this:

tell application "BBEdit"
        set top to true
        repeat
set f to find "[\\da-f]+" searching in text 1 of text document 1 options {search mode:grep, starting at top:top, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
                if (found of f) then
                        set t to found text of f
                else
                        exit repeat
                end if
                set selection of window 1 to my fromHex(t)
                set top to false
        end repeat
end tell

on fromHex(str)
...
end fromHex

But you will have to do some fine tuning of the grep pattern to make it find hex strings and only hex strings. I just used [\da-f]+ for testing. You might also want to use case sensitive:true, match words:true. I don't know what will work best without seeing the files you're working with, but you can figure it out.

On Friday, June 02, 2006, at 08:20AM, Kenneth Prager <[EMAIL PROTECTED]> wrote:

Thanks for the function.

Sorry for being dense but I don't get how to pass the hex string to
the function and then "set the selection to the output of the
function."

Are you implying that I should be able to invoke this function from
within BBEdit or do I just call this function from another
AppleScript?

Thanks,

Ken P.


At 8:06 PM -0400 6/1/06, David Livesay wrote:
This AppleScript function should convert hex strings to decimals:

on fromHex(str)
        set n to 0
        set i to 1
        repeat with a in reverse of every text item of str
                if a is greater than 9 or a is less than 0 then
                        set a to ASCII number of a
                        if a is greater than 70 then
                                set a to a - 87
                        else
                                set a to a - 55
                        end if
                end if
                set n to n + a * i
                set i to i * 16
        end repeat
        return n
end fromHex

You can use find to find and select the hex strings, then set the
selection to the output of the function, then find next. Make sure
you start at the top and don't loop.

On Jun 1, 2006, at 9:23 AM, Kenneth Prager wrote:

Hi All,

I am receiving some status reports with numbers that are in hex
format.  I was planning to use BBEdit to reformat the report to
make it more readable and would like to convert the hex numbers to
decimal using BBEdit, rather than writing an external program.

I know how to find the hex numbers, using grep, but at am at a loss
regarding the best way to convert them.  I figure that one way is
to send the numbers to an external script but am not sure how to
set this up.  Is this the best way to proceed or can the conversion
be done within BBEdit?  (I read through the manual but didn't see
anything in the text conversion section that looked like it would
work.)  Any suggestions would be appreciated.

TIA,

Ken Prager

--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <[EMAIL PROTECTED]>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to:  <[EMAIL PROTECTED]>





--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <[EMAIL PROTECTED]>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to:  <[EMAIL PROTECTED]>


--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <[EMAIL PROTECTED]>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to:  <[EMAIL PROTECTED]>

Reply via email to