Function Utf8tounicode(sTextUtf8 As String) As Wstring Ptr
	Dim As Long iIndex
	Dim As Long shUnicodeSymbol
	Dim As Wstring Ptr ws = Callocate(Len(sTextUtf8)*4+4)
	if ws then
		For i As Long = 0 To Len(sTextUtf8) -1
			If (sTextUtf8[i] And &hF8)  = &hf0 Andalso (sTextUtf8[i+1] And &hC0) = &h80 Andalso (sTextUtf8[i+2] And &hC0) = &h80 Andalso (sTextUtf8[i+3] And &hC0) = &h80 Then
				shUnicodeSymbol = ((sTextUtf8[i] And &h7) Shl 18) Or ((sTextUtf8[i+1] And &h3F) Shl 12) Or (((sTextUtf8[i+2] And &h3F) Shl 6) Or (sTextUtf8[i+3] And &h3F))
				#ifdef __FB_WIN32__
					dim as Long iFirstSG = (((shUnicodeSymbol - &h10000) and &hFFC00) shr 10) + &hD800
					dim as Long iSecondSG = ((shUnicodeSymbol - &h10000) and &h3FF)+&hDC00
					(*ws)[iIndex] = iFirstSG
					(*ws)[iIndex+1] = iSecondSG
					iIndex+=2
					i+=3
					continue for
				#else
					i+=3
				#EndIf
			elseIf (sTextUtf8[i] And &hF0)  = &hE0 Andalso (sTextUtf8[i+1] And &hC0) = &h80 Andalso (sTextUtf8[i+2] And &hC0) = &h80 Then
				shUnicodeSymbol = ((sTextUtf8[i] And &hF) Shl 12) Or (((sTextUtf8[i+1] And &h3F) Shl 6) Or (sTextUtf8[i+2] And &h3F))
				i+=2
			Elseif (sTextUtf8[i] And &hC0)  = &hC0 Andalso (sTextUtf8[i+1] And &hC0) = &h80 Then
				shUnicodeSymbol = ((sTextUtf8[i] And &h1F) Shl 6) Or ((sTextUtf8[i+1] And &h3F))
				i+=1			
			Elseif (sTextUtf8[i] And &h80)  = &h0 Then
				shUnicodeSymbol = sTextUtf8[i]
			Else
				shUnicodeSymbol = &hFFFD
			Endif
			(*ws)[iIndex] = shUnicodeSymbol
			iIndex+=1
		Next
		(*ws)[iIndex] = 0
	else
		print "Allocated memory error in Utf8tounicode!"
	endif
	Return ws
End Function

Function Unicodetoutf8(wsText As Wstring Ptr ) As String
	if wsText then
		Dim As Long iSimbol , iIndex , iLen = Len(*wsText)
		Dim As Byte Ptr psz = Allocate(iLen*4+1)
		if psz then
			For i As Long = 0 To iLen-1
				iSimbol = wsText[i]
				If iSimbol < &h80 Then
					psz[iIndex] = iSimbol
					iIndex+=1
				Elseif iSimbol < &h800 Then
					psz[iIndex] = &hC0 Or (iSimbol Shr 6)
					psz[iIndex+1] = &h80 Or (iSimbol And &h3f)
					iIndex+=2
				Elseif iSimbol < &h10000 Then
					if iSimbol < &hD800 Orelse iSimbol > &hDFFF then
						psz[iIndex] = (&he0 Or (&hF And (iSimbol Shr 12)))
						psz[iIndex+1] = &h80 Or (&h3f And (iSimbol Shr 6))
						psz[iIndex+2] = &h80 Or (iSimbol And &h3F)
						iIndex+=3
					else
						if (iSimbol and &hD800) = &hD800 then
							if i < iLen-1 then
								dim as Long iSecondSG = wsText[i+1]
								if (iSecondSG and &hDC00) = &hDC00 then
									iSimbol -= &hD800
									iSimbol shl= 10
									iSimbol += &h10000
									iSecondSG -= &hDC00
									iSimbol += iSecondSG
									psz[iIndex] = (&hf0 Or (&h7 And (iSimbol Shr 18)))
									psz[iIndex+1] = (&h80 Or (&h3F And (iSimbol Shr 12)))
									psz[iIndex+2] = (&h80 Or (&h3f And (iSimbol Shr 6)))
									psz[iIndex+3] = &h80 Or (iSimbol And &h3F)
									iIndex+=4
									i+=1
									continue for
								EndIf
							EndIf
						EndIf
						psz[iIndex] = &hEF
						psz[iIndex+1] = &hBF
						psz[iIndex+2] = &hBD
						iIndex+=3
					endif
				Elseif iSimbol > &hFFFF andalso iSimbol < &h110000 Then
					psz[iIndex] = (&hf0 Or (&h7 And (iSimbol Shr 18)))
					psz[iIndex+1] = (&h80 Or (&h3F And (iSimbol Shr 12)))
					psz[iIndex+2] = (&h80 Or (&h3f And (iSimbol Shr 6)))
					psz[iIndex+3] = &h80 Or (iSimbol And &h3F)
					iIndex+=4
				Else
					psz[iIndex] = &hEF
					psz[iIndex+1] = &hBF
					psz[iIndex+2] = &hBD
					iIndex+=3
				End If
			Next
			psz[iIndex] = 0
			Function = *Cast(Zstring Ptr, psz)
			Deallocate(psz)
		else
			print "Allocated memory error in Unicodetoutf8!"
		endif
	endif
End Function
