'
' Converters
' version 0.2
' (c) 2009 Stefan Wurzinger
'

#Include Once "general.bi"

Declare Function Str2Int(ByRef tmpStr As String, ByVal ByteAOG As UByte = 0) As UInteger
Declare Function Str2IntBE(ByRef tmpStr As String) As UInteger
Declare Function Str2IntLE(ByRef tmpStr As String) As UInteger
Declare Function Str2LongInt(ByRef tmpStr As String, ByVal ByteAOG As UByte = 0) As ULongInt
Declare Function Str2LongIntBE(ByRef tmpStr As String) As ULongInt
Declare Function Str2LongIntLE(ByRef tmpStr As String) As ULongInt
Declare Function Int2Str(ByVal tmpInt As UInteger, ByVal Length As Integer = 0, ByVal ByteAOG As UByte = 0) As String
Declare Function Int2StrBE(ByVal tmpInt As UInteger, ByVal Length As Integer = 0) As String
Declare Function Int2StrLE(ByVal tmpInt As UInteger, ByVal Length As Integer = 0) As String
Declare Function LongInt2Str(ByVal tmpInt As ULongInt, ByVal Length As Integer = 0, ByVal ByteAOG As UByte = 0) As String
Declare Function LongInt2StrBE(ByVal tmpInt As ULongInt, ByVal Length As Integer = 0) As String
Declare Function LongInt2StrLE(ByVal tmpInt As ULongInt, ByVal Length As Integer = 0) As String
Declare Function StrAsWStr(ByRef tmpStr As String) As WString Ptr

'Declare Function UTF16toUTF8 (Daten As String) As String
'Declare Function UFT16toANSI (Daten As String) As String
'Declare Function UTF8toUTF16 (Daten As String) As String
'Declare Function UTF8toANSI  (Daten As String) As String
'Declare Function ANSItoUTF8  (Daten As String) As String
'Declare Function ANSItoUTF16 (Daten As String) As String
'Declare Function UTF16toAkt  (Daten As String) As String
'Declare Function UTF8toAkt   (Daten As String) As String
'Declare Function ANSItoAkt   (Daten As String) As String
'Declare Function AkttoUTF16  (Daten As String) As String
'Declare Function AkttoUTF8   (Daten As String) As String
'Declare Function AkttoANSI   (Daten As String) As String

Function Str2Int(ByRef tmpStr As String, ByVal ByteAOG As UByte = 0) As UInteger
  If ByteAOG = 0 Then
  	Return Str2IntBE(tmpStr)
  ElseIf ByteAOG = 1 Then
  	Return Str2IntLE(tmpStr)
  Else
  	Return 0
  End If
End Function

Function Str2IntBE(ByRef tmpStr As String) As UInteger
    Dim q As UInteger
    Dim w As UInteger = 0
    Dim l As Integer = Len(tmpStr)
    For q = 1 To l
        w = w Or (tmpStr[q-1] Shl (l-q)*8)
    Next
    Return w
End Function

Function Str2IntLE(ByRef tmpStr As String) As UInteger
    Dim q As UInteger
    Dim w As UInteger = 0
    Dim l As Integer = Len(tmpStr)
    For q = 1 To l
        w = w Or (tmpStr[q-1] Shl (q-1)*8)
    Next
    Return w
End Function

Function Str2LongInt(ByRef tmpStr As String, ByVal ByteAOG As UByte = 0) As ULongInt
	If ByteAOG = 0 Then
		Return Str2LongIntBE(tmpStr)
	ElseIf ByteAOG = 1 Then
		Return Str2LongIntLE(tmpStr)
	Else
		Return 0
	EndIf
End Function

Function Str2LongIntBE(ByRef tmpStr As String) As ULongInt
    Dim q As ULongInt 'uLongInt, damit kein Überlauf bei shl
    Dim w As ULongInt = 0
    Dim l As Integer = Len(tmpStr)
    For q = 1 To l
        w = w Or (tmpStr[q-1] Shl (l-q)*8)
    Next
    Return w
End Function

Function Str2LongIntLE(ByRef tmpStr As String) As ULongInt
    Dim q As ULongInt 'uLongInt, damit kein Überlauf bei shl
    Dim w As ULongInt = 0
    Dim l As Integer = Len(tmpStr)
    For q = 1 To l
        w = w Or (tmpStr[q-1] Shl (q-1)*8)
    Next
    Return w
End Function

Function Int2Str(ByVal tmpInt As UInteger, ByVal Length As Integer = 0, ByVal ByteAOG As UByte = 0) As String
	If ByteAOG = 0 Then
		Return Int2StrBE(tmpInt, Length)
	ElseIf ByteAOG = 1 Then
		Return Int2StrLE(tmpInt, Length)
	Else
		Return ""
	EndIf
End Function

Function Int2StrBE(ByVal tmpInt As UInteger, ByVal Length As Integer = 0) As String
	Dim tmpStr As String = ""
	Do
		tmpStr = Chr(tmpInt And &b11111111) + tmpStr
		If tmpInt = 0 Then Exit Do
		tmpInt = tmpInt Shr 8
	Loop
	If Length > 0 Then tmpStr = RPad(tmpStr,0,Length)
	Return tmpStr
End Function

Function Int2StrLE(ByVal tmpInt As UInteger, ByVal Length As Integer = 0) As String
	Dim tmpStr As String = ""
	Do
		tmpStr += Chr(tmpInt And &b11111111)
		If tmpInt = 0 Then Exit Do
		tmpInt = tmpInt Shr 8
	Loop
	If Length > 0 Then tmpStr = LPad(tmpStr,0,Length)
	Return tmpStr
End Function

Function StrAsWStr(ByRef tmpStr As String) As WString Ptr
    Dim tmp As WString Ptr = Allocate( Len(tmpStr) * Len(WString) )
    Dim SStrOff As UInteger = CInt(StrPtr(tmpStr))
    Dim DStrOff As UInteger = CInt(StrPtr(*tmp))
    Dim SStrLen As UInteger = Len(tmpStr)
    Asm
        mov esi, [SStrOff]
        mov edi, [DStrOff]
        mov ecx, [SStrLen]
        cld
        repnz movsb
    End Asm
    Return tmp
End Function
