Author |
Message |
Craig Hennesse #1 / 12
|
 validation
i have found a validation method using readkey. a few problems. how do i add together the results of many readkeys to get one valid integer? ord(readkey) returns the ascii in integer form. how do i convert this, after being checked, back to a single integer which represents the character? e.g. if i read in the number '7' as a char, ord(7) would give me an integer '55'. how would i convert this number back to an integer of '7' ??? i want an integer, not a char. chr(55) would produce the number '7' but it will be in char format, and i would not be able to add it to the end of another integer to gain a longint. any ideas? souce code available upon request thanx craig
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
A. Moor #2 / 12
|
 validation
Quote:
> i have found a validation method using readkey. a few problems. how do i add > together the results of many readkeys to get one valid integer? ord(readkey) > returns the ascii in integer form. how do i convert this, after being > checked, back to a single integer which represents the character? e.g. > if i read in the number '7' as a char, ord(7) would give me an integer > '55'. how would i convert this number back to an integer of '7' ?
If you have a character, say 'cc', and want to convert it to the corresponding integer 'n' try n := ord(cc)-ord('0'); (* that's a zero *) better is to determine ord('0') once and define it as a constant at the beginning of your program. Hope this helps Al Moore
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Raimo Suoni #3 / 12
|
 validation
Quote: >if i read in the number '7' as a char, ord(7) would give me an integer >'55'. how would i convert this number back to an integer of '7' ??? i want >an integer, not a char. chr(55) would produce the number '7' but it will be >in char format, and i would not be able to add it to the end of another >integer to gain a longint.
ord(Digit)-ord('0') -- Raimo Suonio, Helsinki, Finland
Oikeinkirjoitusohjeita news- ja web-kirjoittajille: http://www.dlc.fi/%7Eexp-1/oikeinkirjoitus.html
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Dr John Stockto #4 / 12
|
 validation
Quote:
>i have found a validation method using readkey. a few problems. how do i add >together the results of many readkeys to get one valid integer? ord(readkey) >returns the ascii in integer form. how do i convert this, after being >checked, back to a single integer which represents the character? e.g. >if i read in the number '7' as a char, ord(7) would give me an integer >'55'. how would i convert this number back to an integer of '7' ??? i want >an integer, not a char. chr(55) would produce the number '7' but it will be >in char format, and i would not be able to add it to the end of another >integer to gain a longint.
To change 55 into 7, subtract 48; to be elegant, subtract Ord('0'), and you will be OK in any implementation which has the digits as consecutive characters; to be ridiculously overcautious, or to allow for Hex, intyger := Pred(Pos(UpCase(Digit), '0123456789ABCDEF') ; For the reverse transformation, ass 40, or add Ord('0'), or index a constant string '0..F'. --
Prof Timo Salmi's Usenet Q&A <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqn.zip> Other TS FAQs : http://www.uwasa.fi/~ts/http/ tsfaq.html quotmarg.html . My page <URL:http://www.merlyn.demon.co.uk/news-use.htm> on usage of News.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Mike Copela #5 / 12
|
 validation
Quote: > i have found a validation method using readkey. a few problems. how do i add > together the results of many readkeys to get one valid integer? ord(readkey) > returns the ascii in integer form. how do i convert this, after being > checked, back to a single integer which represents the character? e.g. > if i read in the number '7' as a char, ord(7) would give me an integer > '55'. how would i convert this number back to an integer of '7' ??? i want > an integer, not a char. chr(55) would produce the number '7' but it will be > in char format, and i would not be able to add it to the end of another > integer to gain a longint.
You have received several responses which deal with the specific question you've asked, but I don't believe they address the whole problem you're dealing with. I suspect you actually should be processing more than single digits - data such as "136", etc. To confine your conversions to individual (digit) characters is pretty shortsighted, I feel, and you should really be thinking of _string_data_, not characters. So, you should be reading in, validating, and converting string variables to numeric values. You can still use ReadKey for such input processing - and you can ignore any characters but '0'..'9' - to construct strings, but you can use the Val procedure to do the conversion to Byte, integer, Word, LongInt, etc. You can also use Readln to fetch while string values and use Val to convert, but you don't need to check individual characters: Val will return an error status if any character(s) in the string aren't numeric digits. If you look up the Val procedure in your manual, you'll see how it's a simple and effective way to read, validate and convert numeric data strings. It's the way many TP/BP programmers do it...
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Craig Hennesse #6 / 12
|
 validation
thanks ill try that craig
Quote:
> > i have found a validation method using readkey. a few problems. how do i add > > together the results of many readkeys to get one valid integer? ord(readkey) > > returns the ascii in integer form. how do i convert this, after being > > checked, back to a single integer which represents the character? e.g. > > if i read in the number '7' as a char, ord(7) would give me an integer > > '55'. how would i convert this number back to an integer of '7' ? > If you have a character, say 'cc', and want to convert it to the > corresponding integer 'n' try > n := ord(cc)-ord('0'); (* that's a zero *) > better is to determine ord('0') once and define it as a constant > at the beginning of your program. > Hope this helps > Al Moore
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
The Scarlet Manu #7 / 12
|
 validation
Quote:
>> if i read in the number '7' as a char, ord(7) would give me an integer >> '55'. how would i convert this number back to an integer of '7' ? > If you have a character, say 'cc', and want to convert it to the > corresponding integer 'n' try > n := ord(cc)-ord('0'); (* that's a zero *) > better is to determine ord('0') once and define it as a constant
More simply you can use the Val procedure to convert any string to a number. This will also perform validation for you (at least, it will check that the character is a digit; if you need it to be in a specific range you'll still need to do that yourself, of course). -- ______________________________________________________________________ The Scarlet Manuka, | Nitpickers' Party motto: Pratchett Quoter At Large, | "He who guards his lips guards his First Prophet of Bonni, is: | soul, but he who speaks rashly will
______________________________|_______________________________________
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Osmo Ronkan #8 / 12
|
 validation
Quote:
>If you have a character, say 'cc', and want to convert it to the >corresponding integer 'n' try >n := ord(cc)-ord('0'); (* that's a zero *) >better is to determine ord('0') once and define it as a constant >at the beginning of your program.
Why would that be better? As TP programs are not going to be run outside ASCII environment, one can safely do: n:=ord(cc)-48; That is far more readable :-) Osmo
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Osmo Ronkan #9 / 12
|
 validation
Quote:
> So, you should be reading in, validating, and converting string >variables to numeric values. You can still use ReadKey for such input >processing - and you can ignore any characters but '0'..'9' - to >construct strings, but you can use the Val procedure to do the conversion >to Byte, integer, Word, LongInt, etc.
Actually Val should not be used with byte, integer or Word but with Longint. After that one should check the proper range. Val converts into longint and then takes necessary amount of low bytes, so var x:byte; err:integer; Begin Val('300',x,err); Writeln(err); Writeln(x); End. Outputs: 0 44 300 mod 256 = 44. One should use some other method, like checking that th length is 2 or under (in which case one would limit the range to 0..99) if one wants to use byte as parameter. Now similar things apply to word and integer. Osmo
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
nathanael joue #10 / 12
|
 validation
Quote:
> i have found a validation method using readkey. a few problems. how do i add > together the results of many readkeys to get one valid integer?
to get a valid integer, make var result : integer; com : char; numb : byte; result:=0; com:='~'; {to avoid com to be setted at "Enter" coz of memory garbage} while com<>"enter" do begin {i never remember enter's code from space's one, it's #13 or #32, eoln should work too} result:=result*10; readkey(com); numb:= ord (com)-ord('0'); {as the other explained it} result:=result+numb; end could improve it to detect if the first strike is a minus sign therefore.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
The Scarlet Manu #11 / 12
|
 validation
Quote:
>> i have found a validation method using readkey. a few problems. how do i add >> together the results of many readkeys to get one valid integer? > to get a valid integer, make > var result : integer; > com : char; > numb : byte; > result:=0; > com:='~'; {to avoid com to be setted at "Enter" coz of memory garbage} > while com<>"enter" do begin {i never remember enter's code from space's > one, it's #13 or #32, eoln should work too} > result:=result*10; > readkey(com); > numb:= ord (com)-ord('0'); {as the other explained it} > result:=result+numb; > end
Why not just join the characters together in a string and use Val? This takes care of the validation and interpretation for you. -- ______________________________________________________________________ The Scarlet Manuka, | Nitpickers' Party motto: Pratchett Quoter At Large, | "He who guards his lips guards his First Prophet of Bonni, is: | soul, but he who speaks rashly will
______________________________|_______________________________________
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
nathanael joue #12 / 12
|
 validation
Quote: > > to get a valid integer, make > > var result : integer; > > com : char; > > numb : byte; > > result:=0; > > com:='~'; {to avoid com to be setted at "Enter" coz of memory garbage} > > while com<>"enter" do begin {i never remember enter's code from space's > > one, it's #13 or #32, eoln should work too} > > result:=result*10; > > readkey(com); > > numb:= ord (com)-ord('0'); {as the other explained it} > > result:=result+numb; > > end > Why not just join the characters together in a string and use Val? This > takes care of the validation and interpretation for you.
I didn't write it, but this method avoid crash causey by "Invalid Numeric Format" when you do a keyrror in your input. Just have to add "if com IN ['0'..'9'] then" before "numb:= ord (com)-ord('0');"
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
|
|