Code archives/Miscellaneous/UTF8 <-> Unicode converter

This code has been declared by its author to be Public Domain code.

Download source code

UTF8 <-> Unicode converter by Space Fractal2008
It seen somebody allready posted such a code by a another user recently, but since I didden't saw that, I dedicated to write my own code about it.

I know this might been slowere than Junkprogger's version, but I want share it anyway.
Function UTF8$(Unicode$)
	Local RESULT$=""
	
	For Local i=1 To Len(Unicode$)
		Local Char$=Mid$(Unicode$,i,1)
		If Asc(Char$)<128
			result$=result$+char$
		ElseIf Asc(Char$)>127 And Asc(Char$)<2048
			Local Bytes$=Right$(Bin$(Asc(Char$)),11)

			Local Byte1$="110"+Left$(Bytes$,5)
			Result$=Result$+Chr(Bin2Int(Byte1$))

			Local Byte2$="10"+Right$(Bytes$, 6)
			Result$=Result$+Chr(Bin2Int(Byte2$))
		Else
			Local Bytes$=Right$(Bin$(Asc(Char$)),16)

			Local Byte1$="1110"+Left$(Bytes$,4)
			Result$=Result$+Chr(Bin2Int(Byte1$))

			Local Byte2$="10"+Mid$(Bytes$, 5, 6)
			Result$=Result$+Chr(Bin2Int(Byte2$))

			Local Byte3$="10"+Right$(Bytes$, 6)
			Result$=Result$+Chr(Bin2Int(Byte3$))		
		EndIf
	Next
	Return Result$
EndFunction

Function Unicode$(UTF8$)
	Local RESULT$=""
	Local UTF$=""
	Local Length=0
	Local Last$=""
	For Local i=1 To Len(UTF8$)
		Local Char$=Mid$(UTF8$,i,1)
		Local B$=Right$(Bin$(Asc(Char$)),8)
		If Length>0
			UTF$=UTF$+Right$(B$,6)
			Length:-1
			If Left$(B$,2)<>"10"
				Length=0
				RESULT$=RESULT$+Last$
				Last$=""
			ElseIf Length=0
				RESULT$=RESULT$+Chr$(Bin2Int(UTF$))
			EndIf
		EndIf
			
		If Length=0
			If Left$(B$,1)="0"
				Result$=Result$+Char$; Length=0
			ElseIf Left$(B$,3)="110"
				Last$=CHAR$
				UTF$=Right$(B$,5)
				Length=1
			ElseIf Left$(B$,4)="1110"
				UTF$=Right$(B$,5)
				Length=2
			ElseIf Left$(B$,4)="11110"
				UTF$=Right$(B$,5)
				Length=3
			EndIf
		EndIf
	Next
	Return RESULT$
EndFunction

' a slow help function ;-), but it do that job
Function Bin2Int:Int(Binary$)
	Local result=0
	Local D=1
	For Local i=Len(Binary$) To 1 Step -1
		If Mid$(Binary$,i,1)="1" Then result=result+d
		D=D+D
	Next
	Return result
EndFunction

Comments

klepto22008
It may be faster if you use this instead of your bin2int function:

result = Int("%"+binary)


Space Fractal2008
Hey I really diddent think about it.

Here I have used somewhere in my jukebox application, where it absolute not required to been fast (the user would not notice it at all).

The last function might been

Function Bin2Int:Int(Binary$)
	Return Int("%"+binary$)
EndFunction


or simply replace that function directly in the code.


Code Archives Forum