Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Byte Array to String

2,363 views
Skip to first unread message

James

unread,
Jan 21, 2006, 12:49:58 PM1/21/06
to
What is the best way to transfer the contents of a ByteArray to a String?

I've tried a for/while loop to empty the contents but have been told using
Mover() is more memory efficient though I cannot get it to work properly.

Thanks.


Avatar Zondertau

unread,
Jan 21, 2006, 1:04:46 PM1/21/06
to

You have to be careful with Move, since the parameters are typeless.
This means strings and dynamic arrays get interpreted in a different
way than you would expect. Instead of the data, the pointer to it is
passed as the parameter. You have to pass to first element instead to
avoid this.

Assuming S is a string and A is a dynamic array of Byte:

SetLength(S, Length(A));
Move(A[0], S[1], Length(S) * SizeOf(S[1]));

danny heijl

unread,
Jan 21, 2006, 3:38:57 PM1/21/06
to
Avatar Zondertau schreef:

> Assuming S is a string and A is a dynamic array of Byte:
>
> SetLength(S, Length(A));
> Move(A[0], S[1], Length(S) * SizeOf(S[1]));

Some instinct tells me to write

SetString(S, PChar(@A[0]), Length(A));

but of course I haven't tested it.

Danny
---

Rudy Velthuis [TeamB]

unread,
Jan 22, 2006, 4:57:55 AM1/22/06
to

SetLength(TheString, Length(TheyArray));
Move(TheArray[0], TheString[1], Length(TheArray));


--
Rudy Velthuis [TeamB] http://rvelthuis.de/

"A lady came up to me on the street, pointed at my suede jacket and
said, "Don't you know a cow was murdered for that jacket?" I said
"I didn't know there were any witnesses. Now I'll have to kill you
too"." -- George Carlin

Rudy Velthuis [TeamB]

unread,
Jan 22, 2006, 5:02:36 AM1/22/06
to

The instinct is wrong. Move requires references, not pointers. The above
would probably not compile (because of the cast, which is not an existing
variable). But if it would, it would only copy junk from the string
variable, and what is beyond that, in memory, to the PChar variable, and
what is beyond that. This would most certainly corrupt memory.

Avatar's syntax was the only correct one.


--
Rudy Velthuis [TeamB] http://rvelthuis.de/

"Anything that is too stupid to be spoken is sung."
- Voltaire (1694-1778)

danny heijl

unread,
Jan 22, 2006, 12:25:07 PM1/22/06
to
Rudy Velthuis [TeamB] schreef:

> The instinct is wrong. Move requires references, not pointers.

Yes, thanks.

Danny
---

Rudy Velthuis [TeamB]

unread,
Jan 22, 2006, 12:58:08 PM1/22/06
to
At 19:45:52, 22.01.2006, danny heijl wrote:

> You'd be surprised that it compiles and works as designed.


>
> > Avatar's syntax was the only correct one.
>

> With Move, yes, with SetString, no.

Oops, I missed the SetString part. Yes, SetString works that way.


--
Rudy Velthuis [TeamB] http://rvelthuis.de/

"A people that values its privileges above its principles soon loses
both."
- Dwight D. Eisenhower (1890-1969), Inaugural Address, January 20, 1953

danny heijl

unread,
Jan 22, 2006, 1:45:52 PM1/22/06
to
Rudy Velthuis [TeamB] schreef:

> The instinct is wrong. Move requires references, not pointers.

But SetString doesn't.

> The above
> would probably not compile (because of the cast, which is not an existing
> variable). But if it would, it would only copy junk from the string
> variable, and what is beyond that, in memory, to the PChar variable, and
> what is beyond that. This would most certainly corrupt memory.

You'd be surprised that it compiles and works as designed.

> Avatar's syntax was the only correct one.

With Move, yes, with SetString, no.

Danny
---

Mike Lischke

unread,
Jan 22, 2006, 1:04:03 PM1/22/06
to
danny heijl wrote

>SetString(S, PChar(@A[0]), Length(A));

If A is nil this code will throw a range check error, because the reference
A[0] is evaluated in any case. Simply use:

SetString(S, Pointer(A), Length(A));

Mike
--
www.soft-gems.net

danny heijl

unread,
Jan 22, 2006, 2:19:52 PM1/22/06
to
Mike Lischke schreef:

> If A is nil this code will throw a range check error, because the reference
> A[0] is evaluated in any case. Simply use:
>
> SetString(S, Pointer(A), Length(A));

I sometimes do, but often I am too lazy to check out if it is safe to
write the pointer cast and end up writing the less efficient code.

Danny
---

0 new messages