Hi Charles,
iconv() works by accepting two pointers (one for the source data, one
for the target data) that are passed by reference. Each time it
converts a character, it increments the corresponding pointer by the
number of bytes needed for the character. (in a single byte encoding,
it'd increase the pointer by 1, in a double-byte, it'd increase by 2, in
a mixed encoding, it increments by 1-4 depending on the number of bytes
needed for the character.)
Pseudo code:
accept input pointer, input space left, output pointer, output space
left as parameters.
do while input/output space left > 0.
read character at input pointer.
increment input pointer by bytesize of character
decrement "input space left" by bytesize of character
translate to output character
write output character to output pointer
increment output pointer
decrement "output space left" by size of output char
enddo
If you know with 100% certainty that the routine will only be used for
single-byte characters, then providing the same space for both input and
output SHOULD work. (But, you _do_ have to provide separate pointers to
that space.)
However, if you have a single buffer, and the input is single byte when
the output is double byte, you'd overwrite some of the bytes in the
buffer before they are read. (Same potential problem could happen with
a mixed encoding.)
IMHO, best practice is to always use separate spaces (or "buffers"..
same thing.) Just in case someone comes along some day and says "I think
we'll use UTF-8 instead of ASCII", and breaks the whole thing.
Note that it doesn't necessarily have to be a USER SPACE... you could
potentially read the data into allocated memory, then use iconv() to
copy it back to the user space. This way, you wouldn't have to create a
second usrspc.
On 5/11/2012 8:11 AM, Charles Wilt wrote:
All,
I've got a user space containing ASCII data. I'm going to use iconv()
to convert the data to EBCDIC.
Do I need a separate userspace to hold the output, or can I simply
send two pointers to inconv() pointing to the same userspace so that
the data is converted in place?
I could see having problems if I was going to/from double byte
<-->single byte. But since I'm going single to single I think it will
work.
We getting ready to try it, but even if it works I want to make sure
it's allowed or recommended and we're not just getting lucky. :)
Thanks!
Charles
As an Amazon Associate we earn from qualifying purchases.