Unit Bit;
Interface
Function ByteToBinary(Value:Byte):String;
Function BinaryToByte(Value:String):Byte;
{각 Value와 같은 형태의 변수를 이진수로 표현되는 문자열로 바꿔준다.}
Implementation
Function ByteToBinary(Value:Byte):String;
Var
bTemp, bValue : Byte;
Begin
Result:= '00000000';
bTemp:= (Value shr 7);
bValue:= Value-(bTemp shl 7);
If bTemp = 1 then Result[1]:= '1';
bTemp:= (bValue shr 6);
bValue:= bValue-(bTemp shl 6);
If bTemp = 1 then Result[2]:= '1';
bTemp:= (bValue shr 5);
bValue:= bValue-(bTemp shl 5);
If bTemp = 1 then Result[3]:= '1';
bTemp:= (bValue shr 4);
bValue:= bValue-(bTemp shl 4);
If bTemp = 1 then Result[4]:= '1';
bTemp:= (bValue shr 3);
bValue:= bValue-(bTemp shl 3);
If bTemp = 1 then Result[5]:= '1';
bTemp:= (bValue shr 2);
bValue:= bValue-(bTemp shl 2);
If bTemp = 1 then Result[6]:= '1';
bTemp:= (bValue shr 1);
bValue:= bValue-(bTemp shl 1);
If bTemp = 1 then Result[7]:= '1';
If bValue = 1 then Result[8]:= '1';
End;
Function BinaryToByte(Value:String):Byte;
Var
Loop : Integer;
Begin
For Loop:= 1 to 8-Length(Value) do Value:= '0'+Value;
Result:= Byte((Value[1] = '1')) shl 7 +
Byte((Value[2] = '1')) shl 6 +
Byte((Value[3] = '1')) shl 5 +
Byte((Value[4] = '1')) shl 4 +
Byte((Value[5] = '1')) shl 3 +
Byte((Value[6] = '1')) shl 2 +
Byte((Value[7] = '1')) shl 1 +
Byte((Value[8] = '1'));
End;
End.
|