全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1066
推到 Plurk!
推到 Facebook!

有關record的轉換問題

答題得分者是:jow
keysound1
一般會員


發表:1
回覆:1
積分:0
註冊:2010-03-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-03-05 14:16:09 IP:114.32.xxx.xxx 訂閱
各位好,小弟第一次接觸PASCAL語言,如果這問題有什麼很好笑的發言還請多包涵。

是這樣的,我宣告了以下的record:


[code delphi]
type
DEVICE_STATE_1 = packed record
Wireless : 0..1;
BlueTooth : 0..1;
Camera : 0..1;
TV : 0..1;
TouchPad : 0..1;
BackLight : 0..1;
LightSensor : 0..1;
D3G : 0..1;
end;
[/code]

如果我沒搞錯的話,這長度應該是1byte。
接下來我有個人家寫死的function叫做A(),他會回傳一個word,
以下的code會造成編譯失敗

[code delphi]
rStatus : DEVICE_STATE_1;
rStatus := A();
[/code]

小弟知道C有彈性可以做這樣的類型轉換,但pascal好像不行?
請問有什麼方法可以解決這個問題?
編輯記錄
keysound1 重新編輯於 2010-03-05 14:19:52, 註解 無‧
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2010-03-05 19:58:29 IP:123.193.xxx.xxx 未訂閱
Pascal 不支援 Bit Field
必須自己處理
以下程式供你參考

[code delphi]
unit fMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TBit = 0..15;

const
Wireless = 0;
BlueTooth = 1;
Camera = 2;
TV = 3;
TouchPad = 4;
BackLight = 5;
LightSensor = 6;
D3G = 7;

type

{ IDeviceStates }
IDeviceStates = interface
function GetStates(b: TBit): Word;
procedure SetStates(b: TBit; Value: Word);
property States[b: TBit]: Word read GetStates write SetStates;
end;

{ TDeviceStates }
TDeviceStates = class(TInterfacedObject, IDeviceStates)
private
FStates: Word;
protected
function GetStates(b: TBit): Word;
procedure SetStates(b: TBit; Value: Word);
public
constructor Create(States: Word);
procedure Assign(Value: Word);
property States[b: TBit]: Word read GetStates write SetStates;
end;

{ TfrmMain }
TfrmMain = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}

{ TDeviceStates }

procedure TDeviceStates.Assign(Value: Word);
begin
FStates := Value;
end;

constructor TDeviceStates.Create(States: Word);
begin
inherited Create;
FStates := States;
end;

function TDeviceStates.GetStates(b: TBit): Word;
begin
Result := (FStates shr b) and $1;
end;

procedure TDeviceStates.SetStates(b: TBit; Value: Word);
begin
if Value > 0 then Value := 1;
FStates := FStates and not ($1 shl b) or (Value shl b);
end;

{ TfrmMain }

procedure TfrmMain.Button1Click(Sender: TObject);
const
FMT = 'Wireless State of $%2.2X = %d';
var
I: Integer;
S: IDeviceStates;
L: TStringList;
begin
L := TStringList.Create;
try
for I := 0 to 31 do
begin
S := TDeviceStates.Create(I);
L.Add(Format(FMT,[I,S.States[Wireless]]));
end;
ListBox1.Items.Text := L.Text;
finally
FreeAndnil(L);
end;
end;

procedure TfrmMain.Button2Click(Sender: TObject);
const
FMT = 'Wireless State of $%2.2X = %d';
var
I: Integer;
S: TDeviceStates;
L: TStringList;
begin
L := TStringList.Create;
try
for I := 0 to 31 do
begin
S := TDeviceStates.Create(I);
try
L.Add(Format(FMT,[I,S.States[Wireless]]));
finally
FreeAndNil(S);
end;
end;
ListBox1.Items.Text := L.Text;
finally
FreeAndnil(L);
end;
end;

procedure TfrmMain.Button3Click(Sender: TObject);
const
FMT = 'Wireless State of $%2.2X = %d';
var
I: Integer;
S: TDeviceStates;
L: TStringList;
begin
L := TStringList.Create;
try
S := TDeviceStates.Create(0);
try
for I := 0 to 31 do
begin
S.Assign(I);
L.Add(Format(FMT,[I,S.States[Wireless]]));
end;
ListBox1.Items.Text := L.Text;
finally
FreeAndNil(S);
end;
finally
FreeAndnil(L);
end;
end;

end.
[/code]

keysound1
一般會員


發表:1
回覆:1
積分:0
註冊:2010-03-05

發送簡訊給我
#3 引用回覆 回覆 發表時間:2010-03-09 19:08:24 IP:114.32.xxx.xxx 訂閱
謝謝您的解答,我自己試試看,如果還有遇到不解的地方再來版上和大家請教
系統時間:2024-05-17 2:32:18
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!