新手问一个简单的问题?关于数据转换(100分)

  • 主题发起人 主题发起人 darkor
  • 开始时间 开始时间
D

darkor

Unregistered / Unconfirmed
GUEST, unregistred user!
二进制的数进行与运算:
如:10111000 and 10000000 得到:10000000。
10111000 and 1000000得到 0 ,怎么实现?
感激不禁。


该死的数字,谁发明的?!郁闷中。。。
 
What does it mean?
 
10111000 | 10111000
10000000 | 01000000
----- | ------
10000000 | 00000000
有点像算IP地址的子码掩护(Subnet)。
 
a:=10111000 b:=10000000
1.(a xor b) xor b
//a:= a xor b
//b:= a xor b
//a:= a xor b
2.(a and b)xor b
 
同意darkor 说的。这是与运算的法则。
 
我得到的数据是16进制的,想得到二进制后,进行如上所述的算法。急着呢!!
望各位高手不吝赐教!
 
给你一个详细的示例!
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

const MinBase = 2;
MaxBase = 36;

function NumToStr (num, len, base: Integer; neg: Boolean;
fill: char): string;
// num = 要转换的数
// len = 生成字符串的最小长度

// base = 进制数 2 = 二进制
// neg = 是否允许负数

// fill = 填充字符用于补满字符串长度//
// 用法:
// NumToStr (45, 8, 2, false, '0') > '00101101'
// NumToStr (45, 4, 8, false, '0') > '0055'
// NumToStr (45, 4, 10, false, ' ') > ' 45'
// NumToStr (45, 4, 16, false, '0') > '002D'
// NumToStr (45, 0, 36, false, ' ') > '19'
//
var s: string;
digit: Integer;
begin
num:= ABS (num);
if ((base >= MinBase) and (base <= MaxBase)) then begin
s:= '';
repeat
digit:= num mod base;
if digit < 10 then Insert (CHR (digit + 48), s, 1)
else Insert (CHR (digit + 55), s, 1);
num:= num div base;
until num = 0;
if neg then Insert ('-', s, 1);
while Length(s) < len do Insert (fill, s, 1);
end;
Result:= s;
end;

//从字符串转换回数:
function StrToNum (const s: string; base: Integer;
neg: Boolean; max: Integer): Integer;
// s = 要转换的字符串
// base = 进制数
// neg = 是否为负数

// max = 要转换的最大数//
// 用法:
// i:= StrToNum ('00101101', 2, false, MaxInt);
// i:= StrToNum ('002D', 16, false, MaxInt);
// i:= StrToNum ('-45', 10, true, MaxInt);
// i:= StrToNum ('ZZ', 36, true, MaxInt);
//
var negate, done: Boolean;
i, ind, len, digit, mmb: Integer;
c: Char;
mdb, res: Integer;
begin
res:= 0; i:= 1; digit:= 0;
if (base >= MinBase) and (base <= MaxBase) then begin
mmb:= max mod base;
mdb:= max div base;
len:= Length (s);
negate:= False;
while (i <= len) and (s = ' ') do Inc (i);
if neg then begin
case s of
'+': Inc (i);
'-': begin Inc (i); negate:= TRUE; end;
end; (* CASE *)
end; (* IF neg *)
done:= len > i;
while (i <= len) and done do begin
c:= Upcase (s);
case c of
'0'..'9': digit:= ORD(c) - 48;
'A'..'Z': digit:= ORD(c) - 55;
else done:= FALSE
end; (* CASE *)
done:= done and (digit < base);
if done then begin
done:= (res < mdb) or ((res = mdb) and (digit <= mmb));
IF done then begin
res:= res * base + digit;
Inc (i);
end; (* IF done *)
end; (* IF done *)
end; (* WHILE *)
if negate then res:= - res;
end; (* IF done *)
Result:= res;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
edit3.Text := numtostr(StrtoNum(edit1.text,2,false,MaxInt) and
strtonum(edit2.text,2,false,MaxInt),10,2,false,'0');
end;

end.
 
后退
顶部