IonicWind Software

IWBasic => General Questions => Topic started by: LarryMc on June 21, 2009, 01:09:19 AM

Title: Conversion Head Scratcher
Post by: LarryMc on June 21, 2009, 01:09:19 AM
Found some code in the old forums that was written in ibasic that I want to use.

I plugged it in to CBasic and it worked fine.

Converted it over to EB and it doesn't work fine.

Adjusted the code in CB so that I could copy the identical code into EB.

The following code produces one result in CB and a different result in EB.

It's doing the calculations different somehow.
Easy way to spot the diff is look at the 1st line of text output.
In the CB version there is a jj around the middle of the line and it isn't in the EB version.

Attached is the jpg I was using for input.

Anyone got any ideas.

   Dim File1 As File
   Dim File3 As Bfile
   Dim Quanta,ByteNo As Int
   Dim OneLine As String
   Dim Byte As Char
Dim OneByte As Char
dim tb,flen as int
   Dim Bytes,ByteCount As Uint

dim fname_in, fname_out as string
fname_in=GETSTARTPATH+"back.jpg"
fname_out=GETSTARTPATH+"back.txt"
   OpenFile(File3,fname_in,"R")
   OpenFile(File1,fname_out,"W")

OneLine=""
flen = 4*Ceil(Len(File3)/4-1)
   For Quanta=0 To flen Step 4
'Read four bytes and convert them to an unsigned integer:
Bytes=0
      For ByteNo=0 To 3
      Read(File3,Byte)
         Bytes=Bytes+256^ByteNo*(Byte&255)
      Next ByteNo
      'Encode the unsigned integer into five characters:
      For ByteNo=4 To 0 Step -1
      OneLine=OneLine+Chr$(Bytes/85^ByteNo+35)
tb=(85^ByteNo)
         Bytes=Bytes% tb 
      Next ByteNo
      If Len(OneLine)=65
      Write File1,OneLine
         OneLine=""
      EndIf
Next Quanta     
   'Write any remaining data:
   If Len(OneLine)
Write File1,OneLine
   EndIf
   CloseFile File3
   CloseFile File1
end
Title: Re: Conversion Head Scratcher
Post by: aurelCB on June 21, 2009, 01:22:42 AM
I dont understand what is the purpose of this program?
Look interesting...
Title: Re: Conversion Head Scratcher
Post by: LarryMc on June 21, 2009, 01:25:24 AM
To encode

But the purpose of the program has no impact on why it gives different results. ;)

Larry
Title: Re: Conversion Head Scratcher
Post by: Ionic Wind Support Team on June 21, 2009, 01:28:21 AM
Don't have time to try it.  But the difference in calculation probably has to do with the divisions.

In Emergence, like most compilers, the result of a division is at the highest precision of the numerator or denominator.  Interpreters and scripting languages usually just do everything in floating point without regard to the type, which is also why they tend to be much slower ;)

1/2 = 0 in Emergence
1/2 = .5 in Creative

Because 1 and 2 are both integers the result is an integer in Emergence/Aurora.  Which is why we have type modifiers:

1/2f = .5
1f/2 = .5

Or

1.0/2.0 = .5

Why would you want to encode 4 bytes into 5?

Paul.
Title: Re: Conversion Head Scratcher
Post by: LarryMc on June 21, 2009, 06:32:32 AM
Quote from: Paul Turley on June 21, 2009, 01:28:21 AM
Why would you want to encode 4 bytes into 5?
Encode binary data into text so that it can be stored like strings.

Larry