View previous topic :: View next topic |
Author |
Message |
schtygnedec
Joined: 23 Jan 2014 Posts: 4
|
Posted: Thu Jan 23, 2014 6:50 pm Post subject: Automatic covertion to ASCII problem |
|
|
Hi, i'm new here
I need to make a program but i have problems with ASCII converting... I need to input the key from number 1 to 100, but if i input letter key, it automatictly converts it to ASCII and that's not what it should be... Please help me, what's the easiest way to disable converting (which action)?
Sorry for my bad english, and thanks in advance |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Wed Jan 29, 2014 4:09 pm Post subject: |
|
|
i have no idea what you mean
where do you get the letter from ?
Do you read this from the serial port or keyboard or what?
Anyway say you have a string with value "100" ., you can use VAL() to convert it to a numeric value.
Say you have "1" you can use ASC() to get the ascii value and the subtract 48 from it.
Just some ideas. _________________ Mark |
|
Back to top |
|
|
schtygnedec
Joined: 23 Jan 2014 Posts: 4
|
Posted: Sat Feb 01, 2014 11:02 pm Post subject: |
|
|
albertsm wrote: | i have no idea what you mean
where do you get the letter from ?
Do you read this from the serial port or keyboard or what?
Anyway say you have a string with value "100" ., you can use VAL() to convert it to a numeric value.
Say you have "1" you can use ASC() to get the ascii value and the subtract 48 from it.
Just some ideas. |
Well i'm a student, working my second program in Bascom...
I need to type the number from 1 to 100 (on keyboard) but there's a problem when i press letter (it's just PC, i'm not programming a microcontroller or smtg)... When i press it program runs like i've typed acsii value (it's converting it automatic) so i need to make IF - ELSE condition to test variable declaring... I just don't know how to write that IF - ELSE condition that could test variable type, that's all
Anyone can give me exaple or smtg? BCZ i DON'T NEED converting to ASCII code or from ASCII code to letter... |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Sun Feb 02, 2014 7:58 pm Post subject: |
|
|
you say on PC but you mean the terminal emulator? when you enter a key on the keyboard of your PC, how will it end up at the micro?
you use some serial connection ?
in that case this would work :
dim x as byte, s as string * 3
do
x=waitkey()
s=s + chr(x)
if len(s)=3 then
x=val(s) 'now the value is stored in byte value into x
print "got " ; x
s="" ' reset for next time
end if
loop _________________ Mark |
|
Back to top |
|
|
schtygnedec
Joined: 23 Jan 2014 Posts: 4
|
Posted: Wed Feb 05, 2014 5:39 pm Post subject: |
|
|
I don't use any connection....
It's just a simple program, here :
Code: |
Dim A As Word
Dim C As Word
Dim X As Word
B:
Print "ENTER A NUMBER IN RANGE FROM 1 TO 100"
Input A
If A >= 1 And A <= 100 Then
For X = A To 100
Print X
Next X
Else
Print "NUMBER IS NOT IN THE RANGE FROM 1 TO 100"
Gosub B
End If
Print "DO YOU WANT TO START PROGRAM AGAIN.TYPE 1 FOR YES ."
Input C
If C = 1 Then
Gosub B
Else
End
End If
|
But when i type letter (lets say "a" ...) it prints from ASCII code number of a to 100, how to edit that (i want to turn it to B when you type letter(string...)? |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Feb 06, 2014 10:58 am Post subject: |
|
|
aha, you mean you want input validation?
in that case you can input a string :
input "number?" , sSomeString
and then you check all characters in the string to be valid. you could use the mid() function for that.
here is some pseudo code:
J=len(sSomestring)
for a=1 to J
krk=mid(sSomestring,a,1) 'get character
p=instr("0123456789", krk) 'get pos
if p=0 then ' illegal data
....
end if
next _________________ Mark |
|
Back to top |
|
|
schtygnedec
Joined: 23 Jan 2014 Posts: 4
|
Posted: Thu Feb 06, 2014 3:33 pm Post subject: |
|
|
Thanks, it works great |
|
Back to top |
|
|
|