A while ago, I posted a question here about using 7 bit and single-bit data
fields with PowerBasic. I didn't really get much of a response here, but I
also posted my question on the PowerBasic tech support BBS, and got a reply
from Mr. John McTaggart.
Here it is, for the information of anyone else who's interested:
BEGIN QUOTE
If you use 7 bits, your available numbers are 0 through 127. The 8th bit
has a value of 128. A routine to encode with Decimal numbers:
DIM NameCode AS BYTE
DIM CustomerCode AS BYTE
.
.
NameCode = [whatever number you came up with 0 - 127]
IF Answer$ = "Yes" THEN
CustomerCode = NameCode
ELSE
CustomerCode = NameCode + 128
END IF
And another routine to decode with Decimal numbers:
IF CustomerCode > 127 THEN
NameCode = CustomerCode - 128
Answer$ = "Yes"
ELSE
NameCode = CustomerCode
Answer$ = "No"
END IF
Just a Quick & Dirty answer to your question, but it should work.
(Refer# 2440)
To: FRANK COX
From: JOHN MCTAGGART
Subj: 7 bit fields, 1 bit field
Read: NO Status: PUBLIC MESSAGE
Conf: Main Board (0) Read Type: MAIL FOR YOU (A) (+)
FC>Does anyone have a routine for creating a file containing 7 bit fields,
FC>and one bit fields? Example follows:
FC>Davey Jones Yes
FC>Susie Smith No
FC>Fred Waters Yes
FC>Joe Schmoe Yes
FC>Now, all of the names could be represented with 7-bit ASCII, and the
FC>yes/no answers with a single bit, 0 or 1. Now, if I wanted to store
FC>this in a file using 7 bit ASCII for the names, and a single bit for
FC>each yes/no answer, how would I go about doing this?
One way to do this would be to just toggle the first, or second bit of
the first letter of the name on and off. Since all the capital and lower
case letters fall between 64 and 127, and all those particular bytes have
identical first and second bits (01) you could just toggle the first bit
to 1 for yes, 0 for no. Example...
Davey Jones would be Äavey Jones for yes, or just Davey Jones for no
D = 0100 0011 (after toggling first bit to 1) = 1100 0011 or -
Susie Smith would be Óusie Smith for yes, or just Susie Smith for no
S = 0101 0011 (after toggling first bit to 1) = 1101 0011 or Ó
When you retrieve the name, you can just test the first letter with
something like bit test and toggle it back to where it was. Of course,
you could also subtract 128 from it and get the same result.
Now, if you really wanta get tricky, you could put up to 4 seperate
tests in those 2 bits. All you need to represent a letter of the
alphabet is the right 6 bits, so you could conceivably code in a scheme
to allow for other things like, are they ugly, are they married, whatever.
Example....
00 - Ugly, Not married
01 - Ugly, Married
10 - Not Ugly, Not Married <
11 - Not Ugly, Married |
|
For Susie Smith it would be ... |
|
S = 0101 0011 (toggle to Not ugly, not married) (10)01 0011 or
usie Smith
After you check the 2 bits you can easily reset it to 01 by toggling
the bits to 0 and 1 by use BIT RESET and BIT SET.
You can also encode messages (whatever) in those spare 2 bits if you
keep it strictly between (64-127). Just remember that it takes 3 pairs
of 2 to represent a letter (assuming all letters start with 01)
Now, if you want to assure net transmission (low ascii) then you can
use a 0 bit for the first bit, the next 6 bits hold the letter, and the
last holds the yes/no bit. It would take a pretty good algorithm to
do this quickly, but I'd image you could use shift left and right
for this ... something like this.
To encode:
Shift left 1 < Shifts the 6 right bits into the middle
Toggle bit 0 to 0
Toggle bit 7 to 1 or 0 for yes/no
To Decode:
Test bit 7 to see what's up yes/no
Shift right 1 < shift the middle 6 to the right (original position)
Reset bit 0 to 0 and bit 1 to 1
Well, I hope you find this useful and that it didn't confuse you more.
Date: 12-05-95 (16:01) Number: 2451 of 2452 (Refer# 25)
To: FRANK COX
From: JOHN MCTAGGART
Subj: Bits
Read: NO Status: PUBLIC MESSAGE
Conf: Main Board (0) Read Type: MAIL FOR YOU (A) (+)
Frank, here is a little program to demonstate what I was talking about in
the other message. Hope it helps...
Defint A-Z
Declare Function BinFmt$(Integer,Integer)
Type Video
Char As Byte
Attr As Byte
End Type
Dim Cell As Shared Video Ptr
Cell = &HB800?? * 65536??
Color 15,1 : Cls
Dim Var As Integer
Var = 65
Locate 2,10: Print " This will take care of all letters A-Z and a-z..."
Locate 4,10: Print " The following is for the letter A "
Locate 6,10: Print " " ; Binfmt$(Var,8) ; " << - before manipulating bits"
Locate 7,10: Print " ^^^^^^ << - the 6 necessary letter bits"
Shift Left Var, 1 'Shift it all left 1
Locate 9,10: Print " " ; Binfmt$(Var,8) ; " bits shifted left 1 ( Shift
Left
Locate 10,10: Print " ^^^^^^ << - 6 letter bits are now in the middle of
the
Bit Reset Var, 7 '< Set bit 7 (left bit) to 0
Locate 12,10: Print " " ; Binfmt$(Var,8)
Locate 13,10: Print " ^ << - reset bit 7 to 0 if low ascii needed ( Bit
Reset
Bit Set Var,0 '< Set bit 0 (right bit) to 1
Locate 15,10: Print " " ; Binfmt$(Var,8)
Locate 16,10: Print " ^ set bit 0 to 1 for yes, 0 for no ( Bit Set
Var
Locate 19,10: Print " Legend: bits are cyan on blue - PB keywords are red
on
Locate 20,10: Print " The 0 and 1 bit are yellow, and ( Var )iables
a
Locate 22,10: Print " Of course there is more than one way to do anything."
For U = 0 to 1999 'Do the whole screen, not efficient, but easy!
Do
'--This will color single characters
Select Case @Cell.Char 'See what the characters are
Case 32,0 'If (space, nul) Then
Exit Do 'We're done, exit loop
Case 48,49 '0 and 1 use yellow
@Cell.Attr = 30 '
Case Else 'Else
Word$ = Word$ + Chr$(@Cell.Char) 'Build the word
End Select 'Doesn't check for punctuation
Incr Cell, 2 'Bump the cell forward
Loop
'--This will color the selected words
Select Case Word$ 'Check the word found
D = Len(Word$) 'Yes, get its length
Decr Cell, 2 * D 'Backup 2 * Len
Case "Shift","Set","Reset","Bit","Left","red"
ColorWord D, 28
Case "bits","cyan"
ColorWord D, 27
Case "Var"
ColorWord D, 26
Case Else
End Select '
Word$="" 'erase it
Incr Cell, 2 'And increment the cell count
Next U
End
Sub ColorWord(L As Integer, Attr As Byte)
Dim Inc As Local Integer
For Inc = 1 to L
@Cell.Attr = Attr
Incr Cell, 2
Next Inc
End Sub
Function BinFmt$(value%,size%)
BinFmt$ = RIGHT$("00000000"+BIN$(value%),size%)
End Function
END QUOTE
--- Msgedsq 2.2e
---------------
* Origin: THE BIG ELECTRIC CAT Melville Sask *SDS* *PDN* (1:140/53)
|