3个回答
2013-04-03
展开全部
unit Convert;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
////////////////////////////////////////////////////////////////////////////////
// DIGITS
// Characters that represent each digit. Add more characters to convert numbers
// with higher bases.
Digits : string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
////////////////////////////////////////////////////////////////////////////////
// COMMON BASES
// Common values for base representations.
cbBin = 2; // Binary base
cbOct = 8; // Octal base
cbDec = 10; // Decimal base
cbHex = 16; // Hexadecimal base
type
////////////////////////////////////////////////////////////////////////////////
// TCONVERT
// Converts a number, in string representation, from a base into many others.
//
// Start
// Place a TConvert in the form. Assign Base property the original base of
// the number to convert, and assign Text the number to convert. Modify Base
// to change the number's representation; use Common Bases constants or set
// your own base.
//
// Properties
// Base - Defines current base representation.
// Text - Stores the number in string format.
TConvert = class(TCustomEdit)
private
{ Private declarations }
FBase : Integer;
FMinDigits : Integer;
procedure SetBase (ABase : Integer);
procedure SetMinDigits (AMinDigits : Integer);
function DigitToValue (Digit : Char) : Integer;
function IsValidBase (Base : Integer) : Boolean;
function PlaceToValue (Place, Base : Integer) : Integer;
function TextToValue (Text : string; Base : Integer) : Integer;
function ValueToText (Value, Base : Integer) : string;
function AdjustValue (Text : string; MinDigits : Integer) : string;
protected
{ Protected declarations }
constructor Create (AOwner : TComponent); override;
public
{ Public declarations }
published
{ Published declarations }
property Base : Integer read FBase write SetBase;
property MinDigits : Integer read FMinDigits write SetMinDigits;
property Text;
property ReadOnly;
property OnChange;
end;
procedure Register;
implementation
procedure TConvert.SetBase (ABase : Integer);
begin
if IsValidBase (ABase) then
begin
Text := AdjustValue (ValueToText (TextToValue (Text, FBase), ABase), MinDigits);
FBase := ABase;
end;
end;
procedure TConvert.SetMinDigits (AMinDigits : Integer);
begin
FMinDigits := AMinDigits;
Text := AdjustValue (ValueToText (TextToValue (Text, Base), Base), MinDigits);
end;
function TConvert.DigitToValue (Digit : Char) : Integer;
var
I : Integer;
begin
Digit := UpCase (Digit);
I := Length (Digits);
while (I > 1) and (Digit <> Digits [I]) do Dec (I);
Result := I - 1;
end;
function TConvert.IsValidBase (Base : Integer) : Boolean;
begin
Result := (Base > 1) and (Base <= Length (Digits));
end;
function TConvert.PlaceToValue (Place, Base : Integer) : Integer;
var
V, I : Integer;
begin
if IsValidBase (Base) then
begin
V := 1;
for I := 2 to Place do V := V * Base;
Result := V;
end else Result := 0;
end;
function TConvert.TextToValue (Text : string; Base : Integer) : Integer;
var
V, I, S : Integer;
begin
if IsValidBase (Base) then
begin
V := 0;
S := Length (Text);
for I := 1 to S do Inc (V, DigitToValue (Text [I]) *
PlaceToValue (S - I + 1, Base));
Result := V;
end else Result := 0;
end;
function TConvert.ValueToText (Value, Base : Integer) : string;
var
T : string;
begin
if IsValidBase (Base) then
begin
if Value = 0 then Result := Digits [1] else
begin
T := '';
while Value > 0 do
begin
T := Digits [(Value mod Base) + 1] + T;
Value := Value div Base;
end;
Result := T;
end;
end else Result := '';
end;
function TConvert.AdjustValue (Text : string; MinDigits : Integer) : string;
var
S : Integer;
begin
S := Length (Text);
if MinDigits > S then
begin
Dec (MinDigits, S);
for S := MinDigits downto 1 do Text := Digits [1] + Text;
end;
Result := Text;
end;
constructor TConvert.Create (AOwner : TComponent);
begin
inherited Create (AOwner);
FBase := 10;
FMinDigits := 8;
end;
////////////////////////////////////////////////////////////////////////////////
procedure Register;
begin
RegisterComponents('Marcalva', [TConvert]);
end;
end.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
const
////////////////////////////////////////////////////////////////////////////////
// DIGITS
// Characters that represent each digit. Add more characters to convert numbers
// with higher bases.
Digits : string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
////////////////////////////////////////////////////////////////////////////////
// COMMON BASES
// Common values for base representations.
cbBin = 2; // Binary base
cbOct = 8; // Octal base
cbDec = 10; // Decimal base
cbHex = 16; // Hexadecimal base
type
////////////////////////////////////////////////////////////////////////////////
// TCONVERT
// Converts a number, in string representation, from a base into many others.
//
// Start
// Place a TConvert in the form. Assign Base property the original base of
// the number to convert, and assign Text the number to convert. Modify Base
// to change the number's representation; use Common Bases constants or set
// your own base.
//
// Properties
// Base - Defines current base representation.
// Text - Stores the number in string format.
TConvert = class(TCustomEdit)
private
{ Private declarations }
FBase : Integer;
FMinDigits : Integer;
procedure SetBase (ABase : Integer);
procedure SetMinDigits (AMinDigits : Integer);
function DigitToValue (Digit : Char) : Integer;
function IsValidBase (Base : Integer) : Boolean;
function PlaceToValue (Place, Base : Integer) : Integer;
function TextToValue (Text : string; Base : Integer) : Integer;
function ValueToText (Value, Base : Integer) : string;
function AdjustValue (Text : string; MinDigits : Integer) : string;
protected
{ Protected declarations }
constructor Create (AOwner : TComponent); override;
public
{ Public declarations }
published
{ Published declarations }
property Base : Integer read FBase write SetBase;
property MinDigits : Integer read FMinDigits write SetMinDigits;
property Text;
property ReadOnly;
property OnChange;
end;
procedure Register;
implementation
procedure TConvert.SetBase (ABase : Integer);
begin
if IsValidBase (ABase) then
begin
Text := AdjustValue (ValueToText (TextToValue (Text, FBase), ABase), MinDigits);
FBase := ABase;
end;
end;
procedure TConvert.SetMinDigits (AMinDigits : Integer);
begin
FMinDigits := AMinDigits;
Text := AdjustValue (ValueToText (TextToValue (Text, Base), Base), MinDigits);
end;
function TConvert.DigitToValue (Digit : Char) : Integer;
var
I : Integer;
begin
Digit := UpCase (Digit);
I := Length (Digits);
while (I > 1) and (Digit <> Digits [I]) do Dec (I);
Result := I - 1;
end;
function TConvert.IsValidBase (Base : Integer) : Boolean;
begin
Result := (Base > 1) and (Base <= Length (Digits));
end;
function TConvert.PlaceToValue (Place, Base : Integer) : Integer;
var
V, I : Integer;
begin
if IsValidBase (Base) then
begin
V := 1;
for I := 2 to Place do V := V * Base;
Result := V;
end else Result := 0;
end;
function TConvert.TextToValue (Text : string; Base : Integer) : Integer;
var
V, I, S : Integer;
begin
if IsValidBase (Base) then
begin
V := 0;
S := Length (Text);
for I := 1 to S do Inc (V, DigitToValue (Text [I]) *
PlaceToValue (S - I + 1, Base));
Result := V;
end else Result := 0;
end;
function TConvert.ValueToText (Value, Base : Integer) : string;
var
T : string;
begin
if IsValidBase (Base) then
begin
if Value = 0 then Result := Digits [1] else
begin
T := '';
while Value > 0 do
begin
T := Digits [(Value mod Base) + 1] + T;
Value := Value div Base;
end;
Result := T;
end;
end else Result := '';
end;
function TConvert.AdjustValue (Text : string; MinDigits : Integer) : string;
var
S : Integer;
begin
S := Length (Text);
if MinDigits > S then
begin
Dec (MinDigits, S);
for S := MinDigits downto 1 do Text := Digits [1] + Text;
end;
Result := Text;
end;
constructor TConvert.Create (AOwner : TComponent);
begin
inherited Create (AOwner);
FBase := 10;
FMinDigits := 8;
end;
////////////////////////////////////////////////////////////////////////////////
procedure Register;
begin
RegisterComponents('Marcalva', [TConvert]);
end;
end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
uses StrUtils; //引用字符串处理单元
function ByteToBin(const AByte: Byte): string;
var
I: Integer;
by: Byte;
begin
Result := '';
I := 0;
by := AByte;
for I := 0 to 8 - 1 do
if ((by shr I) and 1) = 1 then //每次位右移后并1运算,即1 And 1=1 ,0 And 1=0
Result := Result + '1'
else
Result := Result + '0';
Result := StrUtils.ReverseString(Result); //反转字符串
end;
procedure StrToBin(const s: AnsiString);
var
I: Integer;
begin
for I := 1 to Length(s) do //字符串序号是从1开始的
ShowMessage(ByteToBin(Byte(s[1]))); //用Byte进行强制转换
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String str = "ABCDEFG";
String[] c = str.split("");
for(int i = 1; i < c.length; i++){
char ch = c[i].charAt(0);
System.out.println(Integer.toBinaryString(ch));
}
String[] c = str.split("");
for(int i = 1; i < c.length; i++){
char ch = c[i].charAt(0);
System.out.println(Integer.toBinaryString(ch));
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询