Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

PB-DOS: String signed Binary (SSB) data types

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PB-DOS: String signed Binary (SSB) data types

    Routines from the article "Multi-Key Sorting Revisited" by Michael Mattias, published in the May 1997 issue of BASICally Speaking and placed in public domain at that time:

    Code:
    $IF 0
      File: SSBU.BAS
      Functions to convert and process new data type String Signed Binary (SSB)
    
      SSB Format: 32 bit number. Bits 0-30 are the magnitude; the magnitude of
      a negative number is its value + &h 7F FF FF FF  (2,147,000,000+);
      that is, the smallest possible magnitude for a negative SSB is -2 decimal
      The magnitude of minus-1 is x'7F FF FF FE'
      The magnitude of zero or any positive number is its absolute value.
      Bit 31 is a sign bit; 0 for negative numbers, 1 for zero or positive numbers
      Numbers are stored MSB..LSB (NOT INTEL FORMAT)
    
      Purpose of new data type: to allow an ASCII character sort on the four
      bytes to properly arrange numeric values.
    
      3/17/97 started and completed. May be used in FirstBASIC if desired,
      since it uses no unsigned data types, TYPEs or Pointers
    
      6/15/98 Moved to PB 3.5
    
    $ENDIF
    
    $COMPILE UNIT
    $FLOAT EMULATE
    DEFINT A-Z
    
    'ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
    'º    FUNCTION to Convert a long integer into an SSB four-byte string      º
    'ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
    
    FUNCTION LongToSSB$ (L&) PUBLIC
    ' returns a 4-byte string in SSB Format
     LOCAL W&, I%, Buff$
    
     IF L& < pbvZero THEN
        W& = L& + &h07FFFFFFF
     ELSE
        W& = L&
     END IF
    
     Buff$ = SPACE$(04)
    
     FOR I% = 4 TO 1 STEP -1
       MID$(Buff$,I%,1) = CHR$(W& MOD &h100)
       W& = W& \ &h100
     NEXT I%
    
     IF L& >= pbvZero THEN
        MID$(Buff$,1,1) = CHR$(ASC(Buff$) OR &h80)
     END IF
    
     LongToSSB$ = Buff$
    
    END FUNCTION
    
    'ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
    'º    FUNCTION to Convert an SSB four-byte string to a long integer        º
    'ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
    
    FUNCTION SSBToLong& (SSB$)  PUBLIC
    ' input: 4byte string, SSB format. Output: Long Integer.
      LOCAL Work$,Negative%, W&
    
     Work$ = SSB$      ' create local string to avoid messing with original
    
    ' If the high bit of high byte is on, it's a positive number and we need to
    ' mask it off. If not, the number is a negative number biased by &h7FFFFFFF
    
     IF (ASC(SSB$) AND &H80) = &h80 THEN
       MID$(Work$,1,1) = CHR$(ASC(SSB$) AND &h7F)
       Negative% = 0
     ELSE
       Negative% = -1
     END IF
    
     W& = 0
    
     ' take the first three bytes, and multiply into position..
    
     FOR I = 1 TO 3
       W& = W& + ASC(MID$(Work$,I,1))
       W& = W& * 256
     NEXT I
    
     ' add the fourth byte
      W& = W& + ASC(MID$(Work$,4,1))
    
     ' subtract bias if negative..
    
     IF Negative% THEN
       DECR W&, &h7FFFFFFF
     END IF
    
     SSBToLong& = W&
    
    END FUNCTION
    
    
    FUNCTION HexSSB$  (SSB$) PUBLIC
    ' produce a printable hex string to test conversion to SSB data type
    ' input: 4 byte string
    ' output 11 byte string "FF FF FF FF"
    
       Buff$ = ""
       FOR I% = 1 TO 4
        Buff$ = Buff$ + RIGHT$("00" + HEX$(ASC(MID$(SSB$,I%,1))),2)
        IF I% <> 4% THEN
          Buff$= Buff$ + SPACE$(01)
        END IF
      NEXT I%
    
      FUNCTION = Buff$
    
    END FUNCTION

    ------------------
    Michael Mattias
    Tal Systems Inc.
    Racine WI USA
    mailto:[email protected][email protected]</A>
    www.talsystems.com



    [This message has been edited by Michael Mattias (edited October 14, 2003).]
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com
Working...
X