A question about typecasting 
Author Message
 A question about typecasting


Quote:
>Is it possible to typecast a string into and array of char? Or is
there any
>other (more "elegant") solution for that other than using "move" or
copying it
>character by character?

Do you want to convert a string into a null-terminated pchar string?

Assuming you're not using recent delphi versions, where strings are
different:

If you are certain that it will never be longer than 254 chars
(because you need space for the #0) then you can do

s := s+#0;

where s is the string and p is a pChar. It's important for p to skip
s[0] because that is the length byte of the string.

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 A question about typecasting

Quote:

> Is it possible to typecast a string into and array of char?

No. I fear that 'typecast' isn't what you really mean. A typecast in
TP denoted by the syntax some_type(something) is by definition

A) a conversion of a _value_ of one ordinal (enumerable) data type
(integral,char,boolean,enumeration,subrange) or pointer type to a
_value_ of another ordinal data type or pointer type.

        pointer(byte_var) boolean(nil) longint('a') byte($FFFF)

or,

B) a temporary reinterpretion of the bit pattern of a _variable_ of
any type as a _variable_ of any other type at the programmer's risk.
In this case the sizes of the _types_ of the variables - as reported
by the function 'sizeof' - must be identical.

If both interpretations could apply the result is identical and TP
acts as if B) was intended - i.e. a typecast between types of
identical size never changes the bit pattern of the variable/value.

Clearly nothing of this is what you want.

Quote:
> Or is there any
> other (more "elegant") solution for that other than using "move" or copying it
> character by character?

There are several answers. Which you prefer depends on your
application and the reason why you want it.

You may treat a string variable as if it was an array[1..length(s)] of
char, i.e. s[1] is the first character and s[length(s)] is the last
character belonging to the string - and ord(s[0]) = length(s). Thus in
fact a string has some properties of an array of char where the first
char's (s[0]) numeric value indicates the actual number of chars
"belonging" to the string.

If this is not enough to solve your specific problem, my question
would be: why would you want to copy the characters of a string to an
object declared as

        arr:array[x..y] of char

?

If you "just want it because you think you need it" and you don't like
a loop then you might use a lo-level copy

        move(s[1],arr,sizeof(s));

If you use 'move' there is no possible check by the system if the
string will fit into the array. You are copying "raw memory" to "raw
memory" at your own risk.

Regards
Horst



Wed, 18 Jun 1902 08:00:00 GMT  
 A question about typecasting


Quote:
>...
>What I'm doing is using a kind of "large string" (of about 4kb)
represented by
>an "Array [1..4096] of Char" to read "blocks"
>...
>Now suppose the data entered is a string (as it is). Since a string
is
>basically an array of char (with the size at s[0]) I figured there
should be
>a way of assigning it "directly" (not by copying it character by
character
>using a loop).

Would one of the routines in Strings.Pas do? For example, if p -> your
buffer then


would append s to the null-terminated string p, assuming that you know
you have enough room for s. If you have a pointer p1 to the current
end of the string at p then use strlcopy(p1,... and if you know the
space left but don't know if s will fit, null-terminate s (s := s+#0
may be appropriate or you may also have to say something like s[255]
:= #0 if s may be 255 chars already) and then use strlcopy with the
space left instead of length(s). Or use simple strcopy if s is
null-terminated and you're sure there's room. But I would expect
move() to be more efficient.

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Typecast SET to INTEGER ?

2. Invalid Class Typecast

3. SQL-Typecasting to boolean ?

4. value typecasts

5. typecasting preferences

6. Typecasting in OOP

7. Interesting TypeCasting Problem

8. Delphi newbie: Integer to extended typecast

9. Questions, questions, questions....So many questions...Help me please...

10. Questions, questions ...

11. Question on passing pointer to procedure - question.zip (0/1)

12. Question on passing pointer to procedure - question.zip (0/1)

 

 
Powered by phpBB® Forum Software