ascii conversion help needed


Closed Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216

    Default ascii conversion help needed

    Hello all, and A Happy New Year to you all.
    I am reading a device and receive a string of 10 hex characters something like: 0,F,0,2,0,2,A,9,B,1 These are then grouped into 5 2 byte ASCII numbers: 0F,02,02,A9,B1
    So far, so good. This is a serial number of a device.
    My problem is that I need (would like) to convert that to a Decimal unique number.
    Quite obviously, the ascii characters are not necessarily numbers, or even letters for that matter.
    I have tried(and am still trying) to come up with some universal formula which could be applied to any such string that comes in, to convert, but I keep getting What! What! What! What! and that only makes 4 watts, not very bright, huh? apologies to Neddy Seagoon.
    The data is always 10 bytes, converted to 5 characters.
    Any ideas folks??
    Regards, and thanks, as always,
    Peter Moritz,
    Up the bush, By the Sea, South Australia.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    What? Now you have five.

    ... and receive a string of 10 hex characters something like: 0,F,0,2,0,2,A,9,B,1 ...
    ... Quite obviously, the ascii characters are not necessarily numbers, or even letters for that matter.
    Well, if they are HEX characters, then they will always be letters or numbers.

    Are you saying that you want to convert 0F,02,02,A9,B1 into the decimal number 64,458,238,385 ??

    Or do you want to have a 40-bit binary number in 5 bytes like 0F:0202:A9B1 ??

    or is it something else ??
    <br>
    DT

  3. #3
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Thumbs up

    G'day Darrel,
    I have done a bit more on this, and finally got some on screen debugging done.
    What actually comes in is: 0,4,1,4,3,B,B,C,E,6 which I am sending off to EEPROM as 30,34,31,34,33,42,42,43,45,36 which is the ascii of the first lot, correct??
    I have wondered how to send off to EEPROM 0,4,1,4 etc as 04,14, etc making only 5 bytes in EEPROM rather than 10 with half the space used.
    Ending up with 04,14,3B,BC,E6 the decoded ascii for which are all sorts of things. Thus getting back to my first question, getting some sort of decimal number formula that could be used with pretty much any 10 input characters to produce unique values for each.
    I am reading the serial numbers off RFID tags and want to be able to say that card with serial number 04143BBCE6 is user number 356 (or something easily digested)
    As a follow up to that, it would be real good to say that card serial 3BBC67467C is user 1, card 0414BCCD65 is user 2 etc, up to only 7 users for each system.
    Is this confusing you as much as it is me??
    I have played with placing the data in fixed locations in EEPROM, like this. To program the cards in to the thing at first use: Location 1 holds the card serial number in the full 30,34,31, etc format. Location 2 which is 16 further down the eeprom holds the second card's serial. Location 3, another 16 further down holds the third card's serial etc...right down to Location 14 or so, which is the eeprom's maximum space.
    If I could use the half length serial numbers, then I could hold twice the number of cards and therefore users as well as making it easier to compare a newly read card with memory to see if a valid card was read.
    Boy, I'm starting to waffle now, it's getting late.
    PM
    Last edited by muddy0409; - 27th December 2006 at 12:42.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    OK, not a prob.

    Just let the SERIN2 or HSERIN do the work for you...
    Code:
    SerNum  VAR BYTE[5]
    
    SERIN2 Dpin, Dmode, [HEX2 SerNum(4), HEX2 SerNum(3), HEX2 SerNum(2), _
                         HEX2 SerNum(1), HEX2 SerNum(0)]
    Now the SerNum array holds the "half length serial numbers".

    From there you can write it to EEPROM, test it against what's already there, or whatever.

    HTH,
    DT

  5. #5
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    Hmmmm...
    Sort it as it comes in, rather than once it is in..makes sense. I'll give it a try.
    After having slept on it, If I use the known location as the user number, as long as a card reading matches what's in the EEPROM then I can use the location as the ID number. It doesn't really matter what's in there, as long as it is a match.

    Thanks heaps Darrel,
    PM

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    It doesn't really matter what's in there, as long as it is a match.
    Sure, as long as everyone has the same security level. But if there are restrictions like giving access to certain people at certain times of the day, then you'll need to know who they are.

    I'm sure you could do this on your own, but here's a possible search routine. Might save you some time. Haven't thoroughly tested it, but it should be close.
    Code:
    <font color="#000000"><b>Record </b><font color="#008000"><b>VAR BYTE     </b></font><font color="#0000FF"><b><i>; Start of each Record
    </i></b></font><b>Ptr    </b><font color="#008000"><b>VAR BYTE     </b></font><font color="#0000FF"><b><i>; Points to each byte in the record
    </i></b></font><b>TempB  </b><font color="#008000"><b>VAR BYTE     </b></font><font color="#0000FF"><b><i>; Temp var
    </i></b></font><b>MaxEE  </b><font color="#008000"><b>CON </b></font><b>250      </b><font color="#0000FF"><b><i>; Start addr of Last available record
    </i></b></font><b>SerNum </b><font color="#008000"><b>VAR BYTE</b></font>[<b>5</b>]  <font color="#0000FF"><b><i>; The S/N to search for
    </i></b></font><b>UserID </b><font color="#008000"><b>VAR BYTE     </b></font><font color="#0000FF"><b><i>; Resulting User ID
    
    </i></b></font><b>SearchUser</b>:
        <font color="#008000"><b>FOR </b></font><b>Record </b>= <b>0 </b><font color="#008000"><b>TO </b></font><b>MaxEE </b><font color="#008000"><b>STEP </b></font><b>5
            </b><font color="#008000"><b>FOR </b></font><b>Ptr </b>= <b>0 </b><font color="#008000"><b>TO </b></font><b>4
                </b><font color="#008000"><b>READ </b></font><b>Record </b>+ <b>Ptr</b>, <b>TempB
                </b><font color="#008000"><b>IF </b></font><b>SerNum</b>(<b>Ptr</b>) &lt;&gt; <b>TempB </b><font color="#008000"><b>THEN </b></font><b>RecNotMatch
            </b><font color="#008000"><b>NEXT </b></font><b>Ptr
            </b><font color="#008000"><b>GOTO </b></font><b>MatchFound
     RecNotMatch</b>:
        <font color="#008000"><b>NEXT </b></font><b>Record
        UserID </b>= <b>0    </b><font color="#0000FF"><b><i>; returns 0 if no match found
        </i></b></font><font color="#008000"><b>RETURN
        
     </b></font><b>MatchFound</b>:
        <b>UserID </b>= <b>Record </b>/ <b>5 </b>+ <b>1 </b><font color="#0000FF"><b><i>; returns UserID starting at 1
    </i></b></font><font color="#008000"><b>RETURN
    </b></font>
    <br>
    DT

  7. #7
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    G'day Darrel,
    The things you suggested about sorting worked a treat.
    I'm not really interested in 'all the bells and whistles' as far as time zones, access levels etc. as that is already being done by the dozen by everyone else.
    I got a project for just a simple 'valid card = access' type thing, which is pretty hard to find out there.
    Apart from that all the discussion with you has been great. It sure helps to have someone and an excellent forum, to bounce things around in.
    I had the matching already done with the 16 byte data that I had and just gotta change that to 8 byte version.
    I'll be back (probably) when I'm stuck for something else. (probably)
    Thanks again.
    PM

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Ummm, Why 8 bytes?

    If the idea was to store more users in the limited EEPROM space, and you don't need to have other security settings per user. Why not have a 5 byte record size? That's a 60% increase in users. 51 vs 32. (assuming 256 EEPROM)
    <br>
    DT

  9. #9
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    Hi again Darrel,
    All that stuff worked a treat. Thanks.
    Now there is a follow up to that series.
    Now that I am getting 5 bytes to play with, I am simply generating a 4 digit decimal code by taking the last numeral from each of the bytes (decimalised) ((along the lines of dec1 card(1) dec1 card(2) dec1 card(3) dec1 card(4)))
    This works fine, as long as I am sending these numerals out serially or sending to an LCD. What I am having trouble with is sending the bottom half of the decimal byte to a port. The byte should be %0000xxxx where xxxx is the decimal 0 - 9 in binary. Right so far??
    It seems to me that I can't qualify the byte as dec1, BIN4 or anything like that when I am not sending it to the port. I have tried portb = var AND %00001111 which to my way of thinking should mask off the top nibble but what I get out of the port is a full byte of 1's with no resemblance to the numeral that can be deciphered at all.
    I got a few other things to try, so I'll be back.
    I'll have a look at BCD and see if I can make that work.
    Last edited by muddy0409; - 10th January 2007 at 17:17. Reason: more info added

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    What you need is the Bitwise operators "&" + "|" (OR).

    Since you only want to output the lower bits of PORTB, you have to read PORTB first, mask out the lower bits, then OR the new bits in.

    portb = (portb & %11110000) | (var & %00001111)
    <br>
    DT

  11. #11
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel,
    I've been up all nite trying to sort this thing out. Let's see if I can explain where I am ATM:

    I've got 5 bytes coming in as 5 hex2 numbers.
    I am taking the low nibble of each of those as hex1 numerals.
    Gotta then convert then to dec1 digits and write them to port b low nibble.
    I think I've got it sorted...

    I don't think I will need to read, mask, or, then write portb as there is a set sequence that needs following, but that will be another problem, probably.
    I just gotta write the low nibble with the high nibble all 0's and then do some bit manipulation with the high nibble. Should be pretty easy.

    Famous last words huh?

  12. #12
    Join Date
    Jul 2006
    Posts
    2


    Did you find this post helpful? Yes | No

    Default

    Im not sure if this is what you need but, it is a simple Ascii to hex converter. This assumes the stringbyte is limited to valid hex characters.

    ConvertAscii_to_Hex:

    StringByte = StringByte - 48
    Lookup StringByte, ( 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,10,11,12,13,14,15) , Returnbyte

    Return

  13. #13
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    Rightyo, thanks for all the help blokes. I got it all sorted out.
    It may not be pretty, but it works a treat.

    And it fits in the codespace.
    Peter Moritz.
    Up the bush, Western Plains,
    New South Wales,
    Australia.

Similar Threads

  1. ASCII to Long conversion
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 5th March 2009, 23:43
  2. A/D conversion problem in 18F2520, 2523, 2550 etc.
    By selimkara in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 10th March 2008, 16:26
  3. SerIn2 ASCII Conversion
    By rwskinner in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 4th March 2008, 21:41
  4. ASCII Viewer Needed
    By sougata in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 19th January 2008, 05:29
  5. Conversion of 10-bit word into ASCII
    By Barry Johnson in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 16th January 2005, 14:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts