Split Word and merge Bytes
FformulaA
Posts: 9
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?
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
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
I'm going to run with your idea but absolutely I would like to know, just to know it [noparse]:)[/noparse]
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