提问:怎么转化??2进制问题..没有分值了,不好意思(0分)

  • 主题发起人 主题发起人 风零落
  • 开始时间 开始时间

风零落

Unregistered / Unconfirmed
GUEST, unregistred user!
我有7个checkbox。每一个对应一个星期几。。现在为了达到传送目的我用一个2进制数表示这7天的有效问题,字节的每一位对应星期中的一天。如:
0xff=1111111b,
自右至左为星期一到星期天。第八位无效。请问要怎么转化??
 
这个问题很久了,,怎么没有人回答??
 
就是一个位运算的加法问题,有什么难的
i:=0
if checkbox1.checked then i:=i+1
if checkbox2.checked then i:=i+1*2
if checkbox3.checked then i:=i+1*2*2
if checkbox4.checked then i:=i+1*2*2*2
if checkbox5.checked then i:=i+1*2*2*2*2
if checkbox6.checked then i:=i+1*2*2*2*2*2
if checkbox7.checked then i:=i+1*2*2*2*2*2*2
最后就得到i的值,跟二进制转换一模一样,例如得到了15这个值,也就是0001111
说明周一到周四被选中了。很简单
 
TransByte:Byte;
TransByte:=0;
case Day of
1:TransByte:=TransByte or 1;
2:TransByte:=TransByte or 2;
3:TransByte:=TransByte or 4;
4:TransByte:=TransByte or 8;
5:TransByte:=TransByte or 16;
6:TransByte:=TransByte or 32;
7:TransByte:=TransByte or 64;
end;
改一下
 
把期一到星期天的CheckBox的Tag属性依次赋值为1,2,4,8,16,32,64
OnClick事件都指向下面这个函数。
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if TCheckBox(Sender).Checked then
Week:=Week or TCheckBox(Sender).Tag
else
Week:=Week and (not TCheckBox(Sender).Tag);
end;
 
谢谢各位的回答,还是这里的高手多啊
 
TO:Bxch_1000
你得到的结果和我要的不一样,比如说我点击了星期一星期二和星期三。那么我的答案应该 是1
1+2+4,而你的只和点击的顺序有关系,其他的就不行了。。
TO:Liuxudong
你的我也想过了,我也得到过。。不过我不知道怎么限定在我要求的格式中,就是整数和2进制之间的转化,
请求告诉
 
没有分呀:(
 
后退
顶部