STRCONV function in HMG

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
apichit
Posts: 33
Joined: Thu Jun 07, 2012 9:39 am

STRCONV function in HMG

Post by apichit »

Hi All,

May I have some code in HMG to Converts UTF-8 characters in cExpression to double-byte characters.

Regards,
User avatar
Jairo Maia
Posts: 53
Joined: Sun Jul 17, 2011 1:47 pm
Location: Campinas - SP - Brazil

Re: STRCONV function in HMG

Post by Jairo Maia »

Harbour 2.1 or later, have support for UTF-8. There are two functions for conversion:

Hb_UTF8ToStr( cString ) // convert a UTF-8 string to OEM
Hb_StrToUTF8( cString ) // convert a OEM string to UTF-8
apichit
Posts: 33
Joined: Thu Jun 07, 2012 9:39 am

Re: STRCONV function in HMG

Post by apichit »

Hi Jairo Maia,

Thank you very much.

Regards,
Apichit
User avatar
Jairo Maia
Posts: 53
Joined: Sun Jul 17, 2011 1:47 pm
Location: Campinas - SP - Brazil

Re: STRCONV function in HMG

Post by Jairo Maia »

Hi Apichit,

If you need to identifi a coding of the a string, you can to use this function:

Code: Select all

/*
*  This function return the coding of the String.
*
*  You can change as desired, but, the sequence UTF8, ANSI and OEM must
*  be maintained for the function work properly.
*
*  Note: If the return is "DEFAULT", mean that the string have no
*  special character.
*
*  Copyright 2012 Jairo Maia <jairo.s.maia@gmail.com>
*/
Function IdCoding( cString )  // Identifies the coding of a string

   Local i, cIdUtf := Chr( 195 )

   Local aUtf  := { 129, 130, 131, 132, 135, 137, 138, 141, 147, 148, ;
                    149, 150, 154, 156, 160, 161, 163, 162, 164, 167, ;
                    169, 170, 173, 179, 180, 181, 182, 186, 188 }
                    
   Local aAnsi := { 196, 193, 194, 195, 201, 202, 205, 211, 212, 213, ;
                    218, 220, 225, 227, 231, 234, 237, 243, 244, 245, ;
                    246, 250, 252 }

   Local aOem  := { 128, 129, 130, 131, 132, 133, 135, 136, 142, 144, ;
                    147, 148, 153, 154, 160, 161, 162, 163, 181, 182, ;
                    198, 229, 210 }

   If Empty( cString ) .Or. ValType( cString ) != "C"
    Return ("")
   EndIf

   For i = 1 To Len( aUtf )
    If At( cIdUtf + Chr( aUtf[ i ] ), cString ) > 0
     Return ( "UTF8" )
    Endi
   Next

   For i = 1 To Len( aAnsi )
    If At( Chr( aAnsi[ i ] ), cString ) > 0
     Return ( "ANSI" )
    Endi
   Next

   For i = 1 To Len( aOem )
    If At( Chr( aOem[ i ] ), cString ) > 0
     Return ( "OEM" )
    Endi
   Next

Return ( "DEFAULT" )
apichit
Posts: 33
Joined: Thu Jun 07, 2012 9:39 am

Re: STRCONV function in HMG

Post by apichit »

Hi Jairo Maia,
You are so kind.
Thank you very much again. :D :D
Post Reply