BasicMicro - Forums

www.basicmicro.com
It is currently Sun May 20, 2012 11:58 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: A more elegant solution to convert Ascii strings to an int
PostPosted: Mon Aug 29, 2011 2:45 pm 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
I know my questions must seem random. I'm still in the infancy stages of programming, and play with different products in my spare time. I picked up a Parallax RFID reader as we work with RFID quite a bit in my line of work (automation). I have previously taken this technology for granted and just interfaced (expensive) off-the-shelf devices to a PC or PLC.

To the issue at hand... The parallax RFID reader returns 10 alphanumeric hex digits in to a string array. Obviously a 10 digit hex value is too large for a LONG variable, so I have broken it into 2 longs. Is there an easier way to convert ascii hex values to numbers that can be used for math? I can include the rest of the code, but this is the part I'd like to simplify. I'm checking to see if the current BUFFER value is a number or a letter, and then subtracting the appropriate amount to get the hex value.

Code:
BUFFER VAR BYTE(10)
   FOR COUNTER = 0 TO 4
      IF BUFFER(COUNTER) < 58 THEN
         LOWVAL = LOWVAL * 16 + (BUFFER(COUNTER) - 48)
      ELSE
         LOWVAL = LOWVAL * 16 + (BUFFER(COUNTER) - 55)
      ENDIF
      IF BUFFER(COUNTER + 5) < 58 THEN
         HIGHVAL = HIGHVAL * 16 + (BUFFER(COUNTER + 5) - 48)
      ELSE
         HIGHVAL = HIGHVAL * 16 + (BUFFER(COUNTER + 5) - 55)
      ENDIF
   NEXT


If I scan a tag with value 3600A397A8, I will end up with the ascii equivalent of each digit in BUFFER. The code above takes BUFFER and makes 2 long variables containing 3600A and 397A8. And yes, I already noticed my high and low values are swapped with regard to lsb.

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: A more elegant solution to convert ASCII strings to an i
PostPosted: Tue Aug 30, 2011 6:46 am 
Offline
Master

Joined: Tue Nov 21, 2006 9:34 am
Posts: 527
That is more or less the way that I do it, when I don't know for sure the buffer will have the appropriate values... I may use ASCII characters in my test and calculations as to make it easier to read...
Things like:
Code:
     IF BUFFER(COUNTER) <= "9" THEN
         LOWVAL = LOWVAL * 16 + (BUFFER(COUNTER) - "0")

But if your hex strings are being read in by something like a SERIN command you can also do this by using input modifiers. Something like:
Code:
    SERIN ... [hex2 highbyte\2, hex8 lowlong\8]

I made the arbitrary decission to pack the lower 8 hex digits into a long and the top 2 into a byte, but you can choose how many characters to process by each...

Kurt


Top
 Profile  
 
 Post subject: Re: A more elegant solution to convert ASCII strings to an i
PostPosted: Tue Aug 30, 2011 1:01 pm 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
But if your hex strings are being read in by something like a SERIN command you can also do this by using input modifiers. Something like:
Code:
    SERIN ... [hex2 highbyte\2, hex8 lowlong\8]


I am going to give this a try. I like dealing with numbers more so than characters. Thanks for the snippet.

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: A more elegant solution to convert Ascii strings to an i
PostPosted: Tue Aug 30, 2011 1:36 pm 
Offline
Guru

Joined: Mon Apr 19, 2010 9:13 am
Posts: 63
It works... kind of. Studio doesn't allow a minimum modifier on an input, so the line becomes:

Code:
SERIN RXPIN, i2400, [WAIT($0A), HEX2 HIGHVAL, HEX8 LOWVAL]


This works for many cases; tag values that don't start with a 0 as the first digit. If the first digit of the tag is 0, the code breaks. i.e. for tag 0800D9D837, the first 0 gets dropped. So HIGHVAL gets loaded with 80 and LOWVAL with D9D837. Any suggestions for catching that first 0?

_________________
Studio 2.0.0.16 on Win 7 Home x64 updated daily - Original Roboclaw 5A V1.3.9
Mostly a Nano40 for prototyping, but I have others in my bins


Top
 Profile  
 
 Post subject: Re: A more elegant solution to convert Ascii strings to an i
PostPosted: Wed Aug 31, 2011 10:01 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 11:00 am
Posts: 903
Location: Temecula, CA
Read in 8 first and then read in 2. It will exit the read of 2 of you receive something other than a hex number for the second digit(eg you send "02 " it ignores the 0 reads and converts the 2 then sees the space and returns the 2 to the specified variable.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO