Board index » delphi » newbee question,string var to char var

newbee question,string var to char var

right then
its it possible to convert a string containing one thing only, to a char
variable, if so how

im makeing a hash table for a project at school and i want to be able to
enter in someones name into the program and there info will be placed into
the table depending on the characters in there name, i can convert
characters into there ascii numbers and i can break the name down into
single letter strings, but u cant turn the strings into there ascii
numbers, any ideas? im useing the wincrt unit if turbo pascal for windows
if that helps.

smerfy

 

Re:newbee question,string var to char var


If C is a character and S is a string then C := S[1] returns the first
character, C := S[2] returns the second and so forth. S[0] contains a count of
the number of characters in the string.

Quote
smerfy wrote:
> right then
> its it possible to convert a string containing one thing only, to a char
> variable, if so how

> im makeing a hash table for a project at school and i want to be able to
> enter in someones name into the program and there info will be placed into
> the table depending on the characters in there name, i can convert
> characters into there ascii numbers and i can break the name down into
> single letter strings, but u cant turn the strings into there ascii
> numbers, any ideas? im useing the wincrt unit if turbo pascal for windows
> if that helps.

> smerfy

Re:newbee question,string var to char var


Quote
smerfy wrote:

> right then
> its it possible to convert a string containing one thing only, to a char
> variable, if so how

> im makeing a hash table for a project at school and i want to be able to
> enter in someones name into the program and there info will be placed into
> the table depending on the characters in there name, i can convert
> characters into there ascii numbers and i can break the name down into
> single letter strings, but u cant turn the strings into there ascii
> numbers, any ideas? im useing the wincrt unit if turbo pascal for windows
> if that helps.

> smerfy

In early programming the radix 50 method was used to "pack" strings to
numbers. the 50 means octal 50 which is 40 in decimal.

' '      -> 0
'A'..'Z' -> 1..26
'0'..'9' -> 27..37
'.','-'  -> 38,39

Var I : Integer;
    L : Longint;
    S : String[7]; (* ?? *)
Begin
  (* first pad S to with trailing blanks if shorter, not written here *)
  L := 0; I := 1;
  Repeat
    L := L * 40;
    Case S[I] of
    ' ' : ;
    'A'..'Z' : L := L+Ord(S[I])-$40;
    '0'..'9' : L := L+Ord(S[I])-$yyy (* i am too tired to calc that
*)    
    '.'      : L := L+38;
    '-'      : L := L+39;
    Inc(I);
  Until (I > 7); (* ??? *)
  result := L;
End;

Longints are excellent for sorting and searching.

Regards,
Franz Glaser, Austria
http://members.eunet.at/meg-glaser

Re:newbee question,string var to char var


you can't convert a string to a char.  Once the variable has
been declared, you can't very well go and change it
midstream.  But you can take a letter from a string a store
in a char variable.  Or you can access the string variable
as a char type array.

You could get really fancy and do it with pointers, but
why?  

Quote
smerfy wrote:

> right then
> its it possible to convert a string containing one thing only, to a char
> variable, if so how

> im makeing a hash table for a project at school and i want to be able to
> enter in someones name into the program and there info will be placed into
> the table depending on the characters in there name, i can convert
> characters into there ascii numbers and i can break the name down into
> single letter strings, but u cant turn the strings into there ascii
> numbers, any ideas? im useing the wincrt unit if turbo pascal for windows
> if that helps.

> smerfy

Re:newbee question,string var to char var


Bob Lewis <aa...@erols.com> schrieb im Beitrag
<350C6C0C.9348...@erols.com>...

Quote
> > its it possible to convert a string containing one thing only, to a char
> > variable, if so how
<snip>
> If C is a character and S is a string then C := S[1] returns the first
> character, C := S[2] returns the second and so forth. S[0] contains a count
of
> the number of characters in the string.

You should try to avoid the S[0] char to gater the length, use Length(S)
instead. The compiler generates the same code for it. Note that using S[0]
will prevent you to use the code without modification on other or newer
compilers like 32-Bit Delphi.

--
Arsne von Wyss
avonw...@gmx.net

+---------------------------------------------------------------------------+
| Pascal, Delphi & Personal stuff: http://www.beaulieu-software.ch/avonwyss |
|   Swiss IOI'97 homepage: http://www.beaulieu-software.ch/avonwyss/ioi97   |
|  Prog. Contest Problems Archive: http://www.beaulieu-software.ch/contest  |
+---------------------------------------------------------------------------+
         |  "Is that your C program listing or is it line noise?"  |
         +---------------------------------------------------------+

Re:newbee question,string var to char var


i need to use the ord fuction, someone types something in an the program
turns each letter into its ascii value, the problem is the ord fuction
doesnt work with strings

Re:newbee question,string var to char var


In that case you would probably just access the string as a
character array.  Remember that strings have a length byte
at position 0 (the size of the array, so to speak).  If you
need to get the ascii value of character 5, you could do:

if str_var[0] > #4 then  {checks the length...if that's
necessary}
        num := Ord(str_var[5]);

Quote
smerfy wrote:

> i need to use the ord fuction, someone types something in an the program
> turns each letter into its ascii value, the problem is the ord fuction
> doesnt work with strings

Other Threads