Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2004.08.29;
Скачать: CL | DM;

Вниз

в двоичном виде   Найти похожие ветки 

 
appendix ©   (2004-08-11 19:09) [0]

Есть ли в Дельфи средство простое чтоб вывести на экран
вместо 250десятичного число в двоичном виде т е 11111010 ???
Можно конечно и програмку написать, но может есть средсво?


 
Ega23 ©   (2004-08-11 19:16) [1]

Писал не я, но, вроде, работает:

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;


 
Alx2 ©   (2004-08-11 19:30) [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
   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;


 
Alx2 ©   (2004-08-11 19:37) [3]

А вот "классика":

 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;


 
_student   (2004-08-11 19:44) [4]

Вот еще вариант

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 20:15) [5]

>_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;


Может, хватит таблицу умножения в стихах рассказывать? Когда над вопросом думать нечего - начинается "буря в стакане воды".


 
Андрей Сенченко ©   (2004-08-11 20:28) [6]

Потому что чем проще вопрос, тем больше можно предложить решений.
Предлагаю в "потрепаться" открыть ветку сбора вариантов вывода на экран "Hello World". Больше сотни соберем ?



Страницы: 1 вся ветка

Текущий архив: 2004.08.29;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.028 c
10-1034156256
Sikkens
2002-10-09 13:37
2004.08.29
idl2cpp error!!! HELP!


14-1091964298
ИМХО
2004-08-08 15:24
2004.08.29
Смотреть телевидение через компьютер


1-1092414954
Eugene1501
2004-08-13 20:35
2004.08.29
Access violation Как отловить?


14-1088092497
OSokin
2004-06-24 19:54
2004.08.29
Новый сайт


14-1092042550
Klerk
2004-08-09 13:09
2004.08.29
Покупаю монитор. Как протестировать?