![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
в двоичном виде [D5, WinXP]
appendix © (2004-08-11 19:09:00)Есть ли в Дельфи средство простое чтоб вывести на экран
вместо 250десятичного число в двоичном виде т е 11111010 ???
Можно конечно и програмку написать, но может есть средсво?
Андрей Сенченко (2004-08-11 20:28:00) [1]Потому что чем проще вопрос, тем больше можно предложить решений.
Предлагаю в "потрепаться" открыть ветку сбора вариантов вывода на экран "Hello World". Больше сотни соберем ?
Alx2 (2004-08-11 20:15:00) [2]>_student (11.08.04 19:44) [4]
И еще, в пару раз быстрее, чем от [2]
function IntToBinStr(Value: Integer): string;
type
SmallStr = array[0..3] of char;
const
Str: array[0..15] of SmallStr =
(
'0000', '0001', '0010', '0011',
'0100', '0101', '0110', '0111',
'1000', '1001', '1010', '1011',
'1100', '1101', '1110', '1111');
var
ResultArr: array[0..32] of char;
p : Pointer;
begin
SetLength(Result,32);
p := @Result[1];
SmallStr(p^) := Str[(Value shr 28)];
SmallStr(Pointer(Integer(p)+4)^) := Str[(Value shr 24) and $F];
SmallStr(Pointer(Integer(p)+8)^) := Str[(Value shr 20) and $F];
SmallStr(Pointer(Integer(p)+12)^) := Str[(Value shr 16) and $F];
SmallStr(Pointer(Integer(p)+16)^) := Str[(Value shr 12) and $F];
SmallStr(Pointer(Integer(p)+20)^) := Str[(Value shr 8) and $F];
SmallStr(Pointer(Integer(p)+24)^) := Str[Value shr 4 and $F];
SmallStr(Pointer(Integer(p)+28)^) := Str[Value and $F];
end;
Может, хватит таблицу умножения в стихах рассказывать? Когда над вопросом думать нечего - начинается "буря в стакане воды".
_student (2004-08-11 19:44:00) [3]Вот еще вариант
function IntToBin(i:word):String;
var s:string;
mask,j:word;
begin
s:='';
mask:=1 shl 15;
for j:=1 to 16 do
begin
if mask and i > 0 then
s:=s+'1'
else
s:=s+'0';
i:=i shl 1;
if j mod 8 =0 then s:=s+' ';
end;
result:=s;
end;
Alx2 (2004-08-11 19:37:00) [4]А вот "классика":
function ToBin(Val: Integer): string;
var k: Integer;
begin
SetLength(Result, 32);
for k := 32 downto 1 do
begin
Result[k] := chr(Val and 1 + byte('0'));
Val := Val shr 1;
end;
end;
Alx2 (2004-08-11 19:30:00) [5]Число в двоичном виде записать?
Вот вариант с заточкой на скорость:
function IntToBinStr(Value: Integer): string;
type
SmallStr = array[0..3] of char;
const
Str: array[0..15] of SmallStr =
(
'0000', '0001', '0010', '0011',
'0100', '0101', '0110', '0111',
'1000', '1001', '1010', '1011',
'1100', '1101', '1110', '1111');
var
bts: array[0..3] of byte absolute value;
ResultArr: array[0..32] of char;
begin
SmallStr((@ResultArr[0])^) := Str[(bts[3] and $F0) shr 4];
SmallStr((@ResultArr[4])^) := Str[bts[3] and $F];
SmallStr((@ResultArr[8])^) := Str[(bts[2] and $F0) shr 4];
SmallStr((@ResultArr[12])^) := Str[bts[2] and $F];
SmallStr((@ResultArr[16])^) := Str[(bts[1] and $F0) shr 4];
SmallStr((@ResultArr[20])^) := Str[bts[1] and $F];
SmallStr((@ResultArr[24])^) := Str[(bts[0] and $F0) shr 4];
SmallStr((@ResultArr[28])^) := Str[bts[0] and $F];
ResultArr[32] := #0;
Result := ResultArr;
end;
Ega23 (2004-08-11 19:16:00) [6]Писал не я, но, вроде, работает:
Function DecToBin(Dec : integer) : string;
Var
New : string;
// NewTemp : Char;
Temp, x : integer;
begin
// Temp := 0;
Result := '';
while Dec > 1 do
begin
Temp := Dec div 2;
if Dec - Temp * 2 = 0 then Result := Result + '0'
else
Result := Result + '1';
Dec := Temp;
end;
if Dec = 1 then Result := Result + '1';
New := '';
if Length(Result) <> 8 then
for x := Length(Result) to 8 do
Result := Result + '0';
Result:=Copy(Result,1,8);
for x := Length(Result) downto 1 do
New := New + Result[x];
DecToBin := New;
{DecToBin:=Result;}
end;