Shop OBEX P1 Docs P2 Docs Learn Events
Split Word and merge Bytes — Parallax Forums

Split Word and merge Bytes

FformulaAFformulaA Posts: 9
edited 2007-08-20 15:51 in BASIC Stamp
I'm storing a password value of 0000 to 9999 to an eeprom memory. I have a keypad interfaced to a BS2e. For input of the password from the user, I keep looping through key inputs until the keys reach 6 digits or the # key is pressed. How do I merge all these key strokes into a one word variable? Below is the code I'm working with. Keypress is the KEY signal from memKey. 14 is the # key.

Show_Pass:
'does a whole bunch of stuff then
Goto Pass_Check

Pass_Check:
IF keypress = 1 THEN
SERIN TM,396,[noparse][[/noparse]DEC keyin]
IF keyin = 14 THEN 'if # key check serials else loop
'TODO: get the password from memory
'TODO: check the password entered
ELSE
entpassword = entpassword & keyin <
code in question
ENDIF
ENDIF

GOTO Show_Pass

So if the user were to enter 1234 I would like entPassword to be 1234! (MEMKey is setup already to return me the correct numbers for the key pressed).


Just for giggles if I have a word that equals 5432 how can I split the 54 to a byte and 32 to a byte?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-17 20:53
    It's easiest to compute this "on the fly". Say you have a word variable called PASS and you initialize it to 1.
    Every time you get a digit you check to see that PASS < 10000 and treat it as an invalid password if not.
    You then do "PASS = PASS * 10 + keyin". When the "#" comes in, you first check that PASS >= 10000
    and PASS < 20000. This makes sure that a 4 digit number was entered and that leading zeroes were also
    entered. Now you can strip off the leading 1 with "PASS = PASS // 10000" and you've got your password
    as a 4 digit integer value. Note that this works only up to 4 digits because of the limited range of a 16-bit
    word.

    Do you still want to split the first two digits off into a byte and the last two digits into another byte?


    Notice I changed the comment: Every time you get a digit, check for PASS < 10000.· If NOT, the user is trying to enter more than 4 digits.

    Post Edited (Mike Green) : 8/17/2007 10:44:58 PM GMT
  • FformulaAFformulaA Posts: 9
    edited 2007-08-17 21:17
    Amazing! That is a lot better than what I was just trying out! This is my first project in a while 97-98. I haven't seen what the company has been doing for a while and wow has it exploded! Lot of great comp. and devices.

    I'm going to run with your idea but absolutely I would like to know, just to know it [noparse]:)[/noparse]
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2007-08-18 05:14
    To answer your last question for future reference you could use the DIG operator

    lower_half=(word_value DIG 0)+((word_value DIG 1)*10)
    higher_half=(word_value DIG 2)+((word_value DIG 3)*10)
    DEBUG DEC higher_half,CR,DEC lower_half

    Jeff T
  • FformulaAFformulaA Posts: 9
    edited 2007-08-20 15:51
    Thank you Jeff, you have been a tremendous amount of help! I tried it all out and its flawless.
Sign In or Register to comment.