Ascii to Hex conversion

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Tim051
Posts: 4
Joined: Tue Feb 19, 2013 2:50 am

Ascii to Hex conversion

Post by Tim051 » Tue Feb 19, 2013 6:19 am

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Ascii to Hex conversion

Post by Simon » Tue Feb 19, 2013 7:11 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Ascii to Hex conversion

Post by Simon » Tue Feb 19, 2013 7:40 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Tim051
Posts: 4
Joined: Tue Feb 19, 2013 2:50 am

Re: Ascii to Hex conversion

Post by Tim051 » Tue Feb 19, 2013 4:06 pm

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Ascii to Hex conversion

Post by sturgis » Tue Feb 19, 2013 4:29 pm

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?

Tim051
Posts: 4
Joined: Tue Feb 19, 2013 2:50 am

Re: Ascii to Hex conversion

Post by Tim051 » Tue Feb 19, 2013 6:34 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Ascii to Hex conversion

Post by Simon » Tue Feb 19, 2013 8:57 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Tim051
Posts: 4
Joined: Tue Feb 19, 2013 2:50 am

Re: Ascii to Hex conversion

Post by Tim051 » Tue Feb 19, 2013 9:24 pm

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Ascii to Hex conversion

Post by Simon » Tue Feb 19, 2013 9:33 pm

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.
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply