<SOLVED> Iconv Linux how use?

General FreeBASIC programming questions.
Post Reply
VANYA
Posts: 1839
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

<SOLVED> Iconv Linux how use?

Post by VANYA »

Hi All!

How to use libiconv ?

I tried so, but does not work:

Code: Select all

#include "crt/iconv.bi"

dim as wstring ptr wstr0 = @("Hello")

dim as wstring ptr wstr1 = @wstr0 
 
dim as any ptr ic = iconv_open("UTF-8", "UTF-16" ) 
 
dim as zstring ptr psz0 = allocate(50)
dim as zstring ptr psz1 = @psz0 
dim as long l1 = len(*wstr0),l2 = len(*wstr0)
iconv(ic, @psz1, @l1, @wstr1, @l2)

iconv_close(ic)


? *psz0

sleep
Last edited by VANYA on Aug 21, 2017 12:16, edited 1 time in total.
VANYA
Posts: 1839
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Iconv Linux how use?

Post by VANYA »

So something happened, but it is unclear where did the '0' in the rows. Who knows, tell me what's wrong in my code?

Code: Select all

#include "crt/iconv.bi"

dim as wstring*30 ws= "Привет"
? ws
?
dim as wstring ptr wstr0 = @(ws)
 
dim as any ptr ic = iconv_open("UTF-8", "UTF-16" ) 

dim as string*50 zs 

dim as any ptr psz0 = @zs

dim as long l1 = len(ws)*4,l2 =len(ws)*4

iconv(ic, cast(any ptr,@wstr0), @l2, @psz0, @l1)

iconv_close(ic)

? zs
? hex(zs[0]),hex(zs[1]),hex(zs[2]),hex(zs[3]),hex(zs[4]),hex(zs[5]),hex(zs[6]),hex(zs[7]),hex(zs[8])

sleep
Result:
Привет

П р и в е т
D0 9F 0 D1 80 0
D0 B8 0
But should be like this:
D0 9F D1 80 D0 B8
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Iconv Linux how use?

Post by St_W »

Note that WString uses UCS-4 on Linux, see https://freebasic.net/wiki/wikka.php?wakka=KeyPgWstring

So you should be using UTF-32 as source-encoding (instead of UTF-16, which is used by FB on Windows).

Additionally I think that you should be using (U)Integer instead of Long for the variables l1 and l2. Typically size_t is defined as unsigned long in C (which corresponds to unsigned integer in FB). As a general rule use Integer for C's long and Long for C's int.
VANYA
Posts: 1839
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Iconv Linux how use?

Post by VANYA »

St_W wrote:Note that WString uses UCS-4 on Linux, see https://freebasic.net/wiki/wikka.php?wakka=KeyPgWstring

So you should be using UTF-32 as source-encoding (instead of UTF-16, which is used by FB on Windows).
Thank you very much!!!
Post Reply