|
On Mon, 8 Mar 2004, Larry wrote:
>
> I have a problem: I want to create a stream file with data in UTF-8 format.
> Thanks to Scott Klement, I've sorted out my open() O_CCSID vs O_CODEPAGE
> dilemma, but I need to convert my data before I write it to the file.
Why? Why not let open() convert it for you?
> Sounds simple? It should be. The problem is that I know I can use the
> iconv_open(), iconv() and iconv_close() triplets to do the CCSID conversion
> of the data I have sitting on my pointers, but I can't seem to get it
> working. I can perform the open ok (return value of 0). It's calling iconv()
> that is causing me grief.
The following code works for me. But, IMHO, iconv() is an awkward API. I
would strongly recommend getting open() to do the conversion for you
instead, it'll make your code much more readable.
* Quick & Dirty iconv() Demo
*
*
H DFTACTGRP(*NO) ACTGRP(*NEW)
H BNDDIR('QC2LE')
d iconv_t DS based(prototype_only)
d return_value 10I 0
d cd 10I 0 dim(12)
d from DS
d from_CCSID 10I 0
d from_ConvAlt 10I 0 inz(0)
d from_SubsAlt 10I 0 inz(0)
d from_ShiftAlt 10I 0 inz(1)
d from_InpLenOp 10I 0 inz(0)
d from_ErrorOpt 10I 0 inz(1)
D from_Reserved 8A inz(*ALLx'00')
d to DS
d to_CCSID 10I 0
d to_ConvAlt 10I 0 inz(0)
d to_SubsAlt 10I 0 inz(0)
d to_ShiftAlt 10I 0 inz(1)
d to_InpLenOp 10I 0 inz(0)
d to_ErrorOpt 10I 0 inz(1)
D to_Reserved 8A inz(*ALLx'00')
d QtqIconvOpen PR extproc('QtqIconvOpen')
d like(iconv_t)
d tocode like(to) const
d fromcode like(from) const
d iconv PR 10U 0 extproc('iconv')
d cd like(iconv_t) value
d inbuf *
d inbytesleft 10U 0
d outbuf *
d outbytesleft 10U 0
D iconv_close PR 10I 0 extproc('iconv_close')
D cd like(iconv_t) value
*
* MI function to get a hex dump of data in memory
* (target will be twice the size of the source)
*
D HexDump PR ExtProc('cvthc')
D target 32767A options(*varsize)
D src_bits 32767A options(*varsize) const
D length 10I 0 value
D ic ds likeds(iconv_t)
D Data s 12A varying
D p_Data s *
D DataLen s 10U 0
D HexData s 24A
*
* 1252 = MS Windows, US ASCII
* 0 = special value meaning "current job's CCSID"
*
c eval to_CCSID = 1252
c eval from_CCSID = 0
*
* initialize iconv() API
*
c eval ic = QtqIconvOpen(to: from)
c if ic.return_value = -1
** ... FIXME: handle error ...
c endif
*
* Convert "hello world" to Windows ASCII
*
*
c eval Data = 'Hello World!'
c eval p_Data = %addr(Data) + 2
c eval DataLen = %len(Data)
c if iconv(ic: p_Data: DataLen:
c p_Data: DataLen) = -1
** ... FIXME: handle error ...
c endif
*
* Create hex dump to test conversion
*
c callp HexDump( HexData
c : Data
c : %size(HexData))
c dsply HexData
*
* call iconv_close() to free up resources when done
*
c callp iconv_close(ic)
c eval *inlr = *on
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.