Archive: How convert string to UTF-8?


How convert string to UTF-8?
I get path of application data directory by variable $APPDATA and I need write this string in XML file (I use XML NSIS plugin v1.7). Code like this:

${xml::LoadFile} "test.xml" $0
${xml::GotoPath} "/a/c/m" $0
${xml::SetText} "$APPDATA\sdc\Logs\" $0
${xml::SaveFile} "test_saved.xml" $0
${xml::Unload}

But $APPDATA may contented symbols of win1251 code page (russian language), and XML file must be in UTF-8 encoding.

How correctly write strin in xml-file?


Name "Output"
OutFile "Output.exe"

Section
Push "�_усский" #Input text
Push "1251" #Input text code page (multi byte)
Push "65001" #Output text code page (multi byte)
Call ConvertMultiByte
Pop $0 #Output text

MessageBox MB_OK '$$0={$0}'
SectionEnd


Function ConvertMultiByte
Exch $2
Exch
Exch $1
Exch
Exch 2
Exch $0
Exch 2
Push $3
Push $4
Push $5
Push $6
Push $7

StrLen $3 $0
IntOp $3 $3 + 1
IntOp $4 $3 * 2
IntOp $5 $3 * 3

System::Alloc /NOUNLOAD $4
Pop $6
System::Call /NOUNLOAD 'kernel32::MultiByteToWideChar(i $1, i 0, t r0, i r3, i r6, i r3)'

System::Alloc /NOUNLOAD $5
Pop $7
System::Call /NOUNLOAD 'kernel32::WideCharToMultiByte(i $2, i 0, i r6, i r3, i r7, i r5, i 0, i 0)'

System::Call /NOUNLOAD '*$7(&t${NSIS_MAX_STRLEN} .r0)'
System::Free /NOUNLOAD $6
System::Free $7

Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Exch $0
FunctionEnd


Edit:
Working fine now :)

Thx. It's work.

One more question. How it is possible to converte a txt-file in UTF-8?

In my code many lines which need to be converted. And consequently it is easier to write all of them in a file, and converte this file.


http://nsis.sourceforge.net/Unicode_...version_plugin