How to transcode between ANSI anc UTF?

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

How to transcode between ANSI anc UTF?

Postby taijinantangXX » May 23, 2015 2:07

I've noticed that there is a utf_conv.bi include file. There are 4 functions in it. They make me confused and I can't figure out how to use them. Could anyone kindly write some code to show how to use each of them? Or explain what is stand for each parameter?
grindstone
Posts: 644
Joined: May 05, 2015 5:35
Location: Germany

Re: How to transcode between ANSI anc UTF?

Postby grindstone » May 23, 2015 9:46

Hi,

see this little demo:

Code: Select all

#Include "utf_conv.bi"

Dim As String source, destination
Dim As Integer numberOfBytes
Dim As Any Ptr returnValue

Print "UTF_ENCOD_ASCII";UTF_ENCOD_ASCII
Print "UTF_ENCOD_UTF8 ";UTF_ENCOD_UTF8
Print "UTF_ENCOD_UTF16";UTF_ENCOD_UTF16
Print "UTF_ENCOD_UTF32";UTF_ENCOD_UTF32
Print

source = "abc"

Print "source = ";source

destination = String(15,".")
returnValue = CharToUTF(UTF_ENCOD_UTF8,StrPtr(source),Len(source),StrPtr(destination),@numberOfBytes)
' |                            |              |           |              |                |
' |   desired result___________|              |           |              |                |
' |   pointer to source string________________|           |              |                |
' |   number of characters of source string_______________|              |                |
' |   pointer to destination string______________________________________|                |
' |   pointer to variable to receive the length of destination string_____________________|
' |___pointer to destination string (same as parameter 4)   
Print
Print "UTF_ENCOD_UTF8"
Print "        destination = "; destination;"*"
Print "      numberOfBytes ="; numberOfBytes
Print "        returnValue = "; returnValue
Print "StrPtr(destination) = "; StrPtr(destination)

destination = String(15,".")
returnValue = CharToUTF(UTF_ENCOD_UTF16,StrPtr(source),Len(source),StrPtr(destination),@numberOfBytes)
Print
Print "UTF_ENCOD_UTF16"
Print "        destination = "; destination;"*"
Print "      numberOfBytes ="; numberOfBytes
Print "        returnValue = "; returnValue
Print "StrPtr(destination) = "; StrPtr(destination)

destination = String(15,".")
returnValue = CharToUTF(UTF_ENCOD_UTF32,StrPtr(source),Len(source),StrPtr(destination),@numberOfBytes)
Print
Print "UTF_ENCOD_UTF32"
Print "        destination = "; destination;"*"
Print "      numberOfBytes ="; numberOfBytes
Print "        returnValue = "; returnValue
Print "StrPtr(destination) = "; StrPtr(destination)

Sleep

The other functions work analogue to this.

Regards
grindstone
taijinantangXX
Posts: 13
Joined: Dec 19, 2014 5:29

Re: How to transcode between ANSI anc UTF?

Postby taijinantangXX » May 24, 2015 8:13

Thank you very much, grindstone. But I still have problems about using UTFtoChar and UTFtoWchar.
When I used UTFtoChar (and my programme was in UTF-8), I followed your instruction and the bi file to DIM my variables and pointers. The compilation got through without any error or warning, but the output from PRINT got nothing in the console (I did this experiment in WinXP). Could you please tell me the right way of using the 2 functions and show me the right code. Thank you again. (P.S: I suspect I used wrong data type to DIM something or mistake some parameters.)
grindstone
Posts: 644
Joined: May 05, 2015 5:35
Location: Germany

Re: How to transcode between ANSI anc UTF?

Postby grindstone » May 24, 2015 11:06

Here you are:

Code: Select all

#Include "utf_conv.bi"

Dim As String source, destination
Dim As ZString*20 dest
Dim As WString*20 wdest
Dim As Integer numberOfBytes
Dim As Any Ptr returnValue

source = "abc"

Print "source = ";source

destination = String(10,".")
'conversion to UTF-8
returnValue = CharToUTF(UTF_ENCOD_UTF8,StrPtr(source),Len(source),StrPtr(destination),@numberOfBytes)
' |                            |              |           |              |                |
' |   desired result___________|              |           |              |                |
' |   pointer to source string________________|           |              |                |
' |   number of characters of source string_______________|              |                |
' |   pointer to destination string______________________________________|                |
' |   pointer to variable to receive the length of destination string_____________________|
' |___pointer to destination string (same as parameter 4)   
Print
Print "CharToUTF"
Print "  destination = "; destination;"*"
Print "numberOfBytes ="; numberOfBytes
Print "  returnValue = "; returnValue

'back conversion to ASCII
returnValue = UTFToChar(UTF_ENCOD_UTF8,StrPtr(destination),@dest,@numberOfBytes)
Print
Print "UTFToChar"
Print "  destination = "; destination;"*"
Print "         dest = "; dest;"*"
Print "numberOfBytes ="; numberOfBytes
Print "  returnValue = "; returnValue

'back conversion to WString
returnValue = UTFToWChar(UTF_ENCOD_UTF8,StrPtr(destination),@wdest,@numberOfBytes)
Print
Print "UTFToWChar"
Print "  destination = "; destination;"*"
Print "        wdest = "; wdest;"*"
Print "numberOfBytes ="; numberOfBytes
Print "  returnValue = "; returnValue

Sleep

See also these topics:
http://www.freebasic.net/forum/viewtopic.php?t=12979
http://www.freebasic.net/forum/viewtopic.php?f=3&t=5756

If you'd still have problems it would be helpful if you'd post the piece of code that doesn't work.

Regards
grindstone

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 41 guests