Read / Write Power Encoded String
Posted: Sun Jun 20, 2010 1:35 am
Hello everyone,
Ok I know I will not win a nobel price with this one. Anyway have fun !!
Best Regards
Guimauve
Ok I know I will not win a nobel price with this one. Anyway have fun !!
Best Regards
Guimauve
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Read / Write Power Encoded String
; File Name : ReadWritePowerEncodedString.pb
; File version : 1.0.0
; Programmation : OK
; Programmed by : Guimauve
; Date : 19-06-2010
; Mise à jour : 19-06-2010
; PureBasic cade : 4.50
; Plateform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Procedure.q IntegerSquareRoot(Value.q)
A.q = 0
B.q = 6074000999
Root.q = -1
While A <= B
D.q = (A + B) >> 1
D1.q = D * D
If Value > D1
A = D + 1
ElseIf Value < D1
B = D - 1
Else
Root = D
A = B + 1
EndIf
Wend
ProcedureReturn Root
EndProcedure
Procedure.q IntegerCubicRoot(Value.q)
A.q = 0
B.q = 4194303
Root.q = -1
While A <= B
D.q = (A + B) >> 1
D1.q = D * D * D
If Value > D1
A = D + 1
ElseIf Value < D1
B = D - 1
Else
Root = D
A = B + 1
EndIf
Wend
ProcedureReturn Root
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Écriture sur fichier binaire <<<<<
Procedure WritePowerEncodedString(FileID.l, String.s)
StringLen.l = Len(String)
*Source.CHARACTER = @String
WriteLong(FileID, StringLen)
For Index = 1 To StringLen
Char01.c = PeekC(*Source)
*Source + SizeOf(CHARACTER)
If Index % 2 = 0
WriteQuad(FileID, Char01 * Char01 * Char01)
Else
WriteQuad(FileID, Char01 * Char01)
EndIf
Next
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< Lecture sur fichier binaire <<<<<
Procedure.s ReadPowerEncodedString(FileID.l)
StringLen.l = ReadLong(FileID)
String.s = Space(StringLen)
*Source.CHARACTER = @String
For Index = 1 To StringLen
If Index % 2 = 0
Char01.c = IntegerCubicRoot(ReadQuad(FileID))
Else
Char01 = IntegerSquareRoot(ReadQuad(FileID))
EndIf
PokeC(*Source, Char01)
*Source + SizeOf(CHARACTER)
Next
ProcedureReturn String
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< !!! WARNING - TESTING CODE !!! <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Dim Texte.s(5)
Texte(0) = FormatDate("A=%yyyy, M= %mm, J=%dd - %hh:%ii:%ss", Date())
Texte(1) = "PureBasic 4.50 RC2"
Texte(2) = "FreeMat 4.0 (Similiaire à MatLab mais sans Simulink)"
Texte(3) = "Feel the Pure Power !"
Texte(4) = "PureBasic is the best programming language, period !"
Texte(5) = "Linux Ubuntu 10.04 LTS x86_64"
If CreateFile(0, "Test Power Encode.dat")
For Index = 0 To 5
WritePowerEncodedString(0, Texte(Index))
Next
CloseFile(0)
EndIf
If ReadFile(1, "Test Power Encode.dat")
For Index = 0 To 5
Debug Texte(Index)
Debug ReadPowerEncodedString(1)
Debug ""
Next
CloseFile(1)
EndIf
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<