Page 1 of 1

StrDouble() Procedure

Posted: Tue Oct 08, 2002 11:18 pm
by BackupUser
Code updated for 5.20+ (same as StrD())

Restored from previous forum. Originally posted by freak.

Hi All,

If you get a Double LONG Value from an API procedure, and want to
display it as a string, here's a way to do it:

UPDATE: The code had a little bug, that i just corrected.

Code: Select all

Procedure.s StrDouble(lowLONG.l, hiLONG.l)
  Protected buffer1.s, buffer2.s, buffer3.s, pos1.l, pos2.l, pos3.l, calc1.l, calc2.l
  buffer3 = StrU(lowLONG, #PB_Long)
  
  For pos1 = Len(StrU(hiLONG, #PB_Long)) To 1 Step -1
    For pos2 = Len(StrU(-1, #PB_Long)) To 1 Step -1
      
      calc1 = Val(Mid(StrU(hiLONG, #PB_Long), pos1, 1)) * Val(Mid(StrU(-1, #PB_Long), pos2, 1))
      calc2 = (Len(StrU(hiLONG, #PB_Long)) - pos1) + (Len(StrU(-1, #PB_Long)) - pos2)
      
      buffer1 = StrU(calc1) + Left("00000000000000000000",calc2)
      buffer2 = buffer3
      buffer3 = ""
      calc1 = 0
      
      If Len(buffer1) > Len(buffer2)
        buffer2 = Right("00000000000000000000" + buffer2, Len(buffer1))
      Else
        buffer1 = Right("00000000000000000000" + buffer1, Len(buffer2))
      EndIf
      
      For pos3 = Len(buffer1) To 1 Step -1
        calc1 + Val(Mid(buffer1, pos3,1)) + Val(Mid(buffer2, pos3,1))
        buffer3 = Right(Str(calc1), 1)+buffer3
        calc1/10
      Next pos3
      
      If calc1 > 0: buffer3 = Str(calc1) + buffer3: EndIf
      
    Next pos2
  Next pos1
  
  While Left(buffer3,1)="0"
    buffer3 = Right(buffer3, Len(buffer3) - 1)
  Wend
  
  ProcedureReturn buffer3
EndProcedure

; -----------------------------------
; Example getting the Size of Drive C
; -----------------------------------

drive.s = "C:\"

Structure _Quad
  L1.l 
  L2.l
EndStructure

; result is off by 20 bytes... (at least on my machine)
GetDiskFreeSpaceEx_(@drive, FB._Quad, TB._Quad, TFB._Quad)
MessageRequester("Drive Size: ", StrDouble(TB\L1,TB\L2) + " Bytes",0)

;- using PB's build-in StrD... (shows the correct value)
GetDiskFreeSpaceEx_(@drive, @FBQ.q, @TBQ.q, @TFBQ.q)
MessageRequester("Drive Size: ", StrD(TBQ) + " Bytes",0)
End
----

Timo

Posted: Wed Oct 09, 2002 4:40 am
by BackupUser
Restored from previous forum. Originally posted by PB.

When I used this on my NTFS drive the size reported was very wrong.
My drive is 17,240,154,112 bytes capacity, but this procedure says
it's 16,240,153,108 bytes...?

Also, I have 2 x FAT32 drives and this procedure is off by 2 bytes on
the second one... the second is 9,778,479,104 bytes capacity, but this
procedure says it's 9,778,479,102 bytes... any ideas?

We seem to be getting very close, though! :) Thanks.


PB - Registered PureBasic Coder

Posted: Wed Oct 09, 2002 6:31 am
by BackupUser
Restored from previous forum. Originally posted by freak.

Ok PB, I've found some little bug, wich has caused the difference in your
Drive capacyties.

But on my maching, too it's still wrong by one byte, but I don't know why.
Maybe API isn't compleetly accurate here?

Timo

Posted: Wed Oct 09, 2002 7:38 am
by BackupUser
Restored from previous forum. Originally posted by PB.

> on my machine, too it's still wrong by one byte, but I don't know
> why. Maybe API isn't compleetly accurate here?

Your fixed code is still 2 bytes out for me, but only on my 9 GB drive.
Hmm, isn't there a BIOS problem with some motherboards, where a drive
bigger than 8 GB has issues? Maybe that's why your code reports the
correct size for my 3 GB drive, but not my 9 GB? Possibly?


PB - Registered PureBasic Coder

Posted: Wed Oct 09, 2002 7:57 am
by BackupUser
Restored from previous forum. Originally posted by Berikco.

Here, 9 bytes off @ 40GB drive
4 bytes off @ 20 GB drive
all NTFS

Regards,

Berikco

http://www.benny.zeb.be

Posted: Wed Oct 09, 2002 7:06 pm
by BackupUser
Restored from previous forum. Originally posted by cor.

Report:

drive c: 4,87 GB 1 byte less -> 5328435840: 5238435839 4736192512: 4736192511 <- result

Win98 SE FAT 32

Using Windows 98 SE
Registered PB version : 3.30 (Windows)
--------------------------
C. de Visser
Author of Super Guitar Chord Finder
http://www.ready4music.com

Posted: Wed Oct 09, 2002 7:35 pm
by BackupUser
Restored from previous forum. Originally posted by Pupil.

Ok, i've got a theory. It seems as if this StrDouble() procedure is off by 1 byte/4GB i.e. if you've got a disk that has the size 9 GB you get 2 bytes less (9GB/4GB=2.??), a disk size of 23GB is off 5 bytes(23/4=5.???).