我正好做了一个类可以实现你要的功能,可以参考一下。我是二维的,你只要把RowCount设成1就是你要的。
unit BitsArrayClass;
interface
uses Classes, SysUtils, Types, windows;
type
TByteArrayOfArray = array of array of Byte;
TBitsArray = class(TObject)
private
function GetByteValue(Row, Col: Integer): Byte;
procedure SetByteValue(Row, Col: Integer
const Value: Byte);
procedure SetColCount(const Value: Word);
procedure SetRowCount(const Value: Word);
procedure ResizeArray;
protected
FColCount: Word;
FRowCount: Word;
function LoadFromStream(Stream: TStream): Boolean;
function SaveToStream(Stream: TStream): Boolean;
public
BitsArray: TByteArrayOfArray;
constructor Create
virtual;
destructor Destroy
override;
procedure Assign(AObject: TBitsArray);
procedure SetArraySize(RowCount, ColCount: Integer);virtual;
property ByteValue[Row, Col: Integer]: Byte read GetByteValue
write SetByteValue;
property ColCount: Word read FColCount write SetColCount;
property RowCount: Word read FRowCount write SetRowCount;
end;
function BitsArrayLoadFromStream(Stream: TStream;
var BitsArray: TByteArrayOfArray): Boolean;
function BitsArraySaveToStream(Stream: TStream;
const BitsArray: TByteArrayOfArray): Boolean;
function SetBitsArraySize(var BitsArray: TByteArrayOfArray;
RowCount, ColCount: Integer): Boolean;
implementation
{ TBitsArray }
//提取指定字节得某位的值
function ConvertBitsToByte(const AByte: Byte
BitsPos: Byte): Byte;
begin
Result := (AByte shr BitsPos) and 1;
end;
//使得字节指定位的值为1其他为0
function MoveOneByBitsPos(BitsPos: Word): Byte;
begin
Result := (1 shl BitsPos) xor $FF;
end;
function ConvertByteToBits(const AByte: Byte
BitsPos: Integer): Byte;
begin
Result := AByte shl BitsPos;
end;
function SetBitsArraySize(var BitsArray: TByteArrayOfArray;
RowCount, ColCount: Integer): Boolean;
begin
Result := True;
try
SetLength(BitsArray, RowCount, (ColCount - 1) div 8 + 1);
except
Result := False;
end;
end;
function BitsArrayLoadFromStream(Stream: TStream;
var BitsArray: TByteArrayOfArray): Boolean;
var
Row, RowCount, ColCount: Integer;
begin
Result := BitsArray <> nil;
with Stream do
try
RowCount := Length(BitsArray);
ColCount := Length(BitsArray[0]);
for Row := 0 to RowCount - 1 do
Read(BitsArray[Row][0], ColCount);
except
Result := False;
end;
end;
function BitsArraySaveToStream(Stream: TStream;
const BitsArray: TByteArrayOfArray): Boolean;
var
Row, RowCount: Integer;
begin
Result := BitsArray <> nil;
if Result then
with Stream do
try
RowCount := Length(BitsArray);
for Row := 0 to RowCount - 1 do
Write(BitsArray[Row][0], Length(BitsArray[0]));
except
Result := False;
end;
end;
constructor TBitsArray.Create;
begin
FRowCount := 8;
ColCount := 8;
end;
destructor TBitsArray.Destroy;
begin
BitsArray := nil;
inherited;
end;
procedure TBitsArray.SetArraySize(RowCount, ColCount: Integer);
begin
FRowCount := RowCount;
FColCount := ColCount;
SetBitsArraySize(BitsArray, RowCount, ColCount);
end;
function TBitsArray.GetByteValue(Row, Col: Integer): Byte;
var
BytePos: Integer;
BitsPos: Byte;
begin
BytePos := Col div 8;
BitsPos := Col mod 8;
Result := ConvertBitsToByte(BitsArray[Row][BytePos], BitsPos);
end;
procedure TBitsArray.SetByteValue(Row, Col: Integer
const Value: Byte);
var
BytePos, BitsPos: Integer;
AByteValue: Byte;
begin
BytePos := Col div 8;
BitsPos := Col mod 8;
//先把要设置的那一位置成0
AByteValue := BitsArray[Row][BytePos] and MoveOneByBitsPos(BitsPos);
BitsArray[Row][BytePos] := AByteValue or ConvertByteToBits(Value, BitsPos);
end;
procedure TBitsArray.SetColCount(const Value: Word);
begin
if FColCount <> Value then
begin
FColCount := Value;
ResizeArray;
end;
end;
procedure TBitsArray.SetRowCount(const Value: Word);
begin
if FRowCount <> Value then
begin
FRowCount := Value;
ResizeArray;
end;
end;
function TBitsArray.LoadFromStream(Stream: TStream): Boolean;
begin
Result := BitsArrayLoadFromStream(Stream, BitsArray);
end;
function TBitsArray.SaveToStream(Stream: TStream): Boolean;
begin
Result := BitsArraySaveToStream(Stream, BitsArray);
end;
procedure TBitsArray.ResizeArray;
begin
SetArraySize(FRowCount, FColCount);
end;
procedure TBitsArray.Assign(AObject: TBitsArray);
begin
BitsArray := Copy(AObject.BitsArray);
SetArraySize(AObject.RowCount, AObject.ColCount);
end;
end.