Page 1 of 1
Ascii to Hex conversion
Posted: Tue Feb 19, 2013 6:19 am
by Tim051
Hi Folks,
Newbie here!
I've got my first card all done with an option button that let me select 23 values, from 1 to 23. (actually, there's quite a bit more, but let's just stick with this one example)
I would like to convert these ascii values that get returned to hex - for instance, a 09 to 09, 10 to 0A, etc.
I've tried the binaryDecode function, and it will convert the number 09 to a 30 39, which is not what I need.
I've looked through all of the FAQs, and searched the forums, but haven't found the magic decoder ring.
Any helpful hints would be greatly appreciated.
Thanks!
Tim
Re: Ascii to Hex conversion
Posted: Tue Feb 19, 2013 7:11 am
by Simon
Hi Tim,
Code: Select all
on menuPick pItemName
put format("%02x",pItemName) into tHex
answer tHex
end menuPick
That should do it.
Check out "format" in the dictionary, lots of fun stuff to use it for.
Simon
Re: Ascii to Hex conversion
Posted: Tue Feb 19, 2013 7:40 am
by Simon
I just found "baseConvert" it will do hex for you.
But if you need 2 digits then you have to add a leading 0.
Simon
Re: Ascii to Hex conversion
Posted: Tue Feb 19, 2013 4:06 pm
by Tim051
Hi Simon,
Thanks for the info. I tried them out, and indeed, it does return a hex value.
However, when I write the data out to a file, the '0A' (for 10). becomes 30 41 !! Guess what I 'really' need is BCD!
Tim
Re: Ascii to Hex conversion
Posted: Tue Feb 19, 2013 4:29 pm
by sturgis
How are you writing to the file? With open file, or put URL .. Mind sharing your code for that part, as well as the end goal?
Re: Ascii to Hex conversion
Posted: Tue Feb 19, 2013 6:34 pm
by Tim051
Hi Sturgis,
Thanks for the note.
The main screen has 16 buttons which select the 'Hours' and 'Minutes' for 8 alarms on a project I'm working on. The hours pulldown buttons are 0-23, and the minutes are 0-59. There is also a 'send message' button which takes each of the hours and minutes, puts them into a file, which is saved onto the computer.
Each entry is placed into an array called 'test', and when I press the send button, it takes the array one by one, and places into a variable:
repeat with i=0 to 16
put test after packet -- where packet is the file that I save to the pc.
(I started to use the 'combine' instruction to convert the array to a 'plain' variable, but it didn't put them in the right order, so I resorted to the 'brute force' method). (The dictionary says: Note: The order of the elements is not alphabetical or chronological; it is based on the internal hash order of the array. - a bit worthless for this)!
The file consists of 18 bytes, the first being a fixed number, followed by the number of bytes (in hex), followed by the hr-min of each alarm.
So, looking at the generated file with a hex editor, I would see the following: 65 12 01 23... (65 is a necessary prefix, 12 is # of bytes (in hex), the 01 23 represents the first alarm which is set for 01:23. So the data is in BCD.
To do the file writing, I have taken one of the examples from the videos:
put specialFolderPath ("desktop") & "/testfile.sbd" into tFileName
put packet into url ("file:" & tFileName)
That's it.
Thanks,
Tim
Re: Ascii to Hex conversion
Posted: Tue Feb 19, 2013 8:57 pm
by Simon
Hi Tim,
However, when I write the data out to a file, the '0A' (for 10). becomes 30 41 !!
Not sure I follow you on this, when I use:
Code: Select all
on menuPick pItemName
put format("%02x",pItemName) into tHex
put tHex into url("file:myFile.txt")
end menuPick
for 10 I get 0a in myFile.txt and ff for 255, don't think it could be a platform issue.
Are you making any other conversions after this initial one?
Simon
Re: Ascii to Hex conversion
Posted: Tue Feb 19, 2013 9:24 pm
by Tim051
Hi Simon,
I put your code in, and indeed, if I select a '12' on the button, a 0c gets written to the text file.
The problem is that each of these characters - 0 & c are handled as separate characters. If I open the file with my hex debugger, it shows two separate characters, a 30, and a 63.
What I need is a single character written to the file as a 0c.
BTW, thanks!
Tim
Re: Ascii to Hex conversion
Posted: Tue Feb 19, 2013 9:33 pm
by Simon
OK a wild guess here
Try putting "#" before the 0c as in:
#0c
Simon
EDIT: or is it 0x?
Note the # sign above needs to be added this way:
put "#" before tHEx
Maybe look at the Hex code that shows up correctly in your editor with a text editor instead, see if there are hints there.