Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2004.08.29;
Скачать: [xml.tar.bz2];

Вниз

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

 
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;
Скачать: [xml.tar.bz2];

Наверх




Память: 0.46 MB
Время: 0.036 c
14-1092077360
gn
2004-08-09 22:49
2004.08.29
500 ДОЛЛАРОВ США ЗА ОБНАРУЖЕНИЕ ДЫРЫ В БРАУЗЕРЕ MOZILLA


14-1092019928
inew
2004-08-09 06:52
2004.08.29
Начальник уходит на повышение.


1-1092146582
andy
2004-08-10 18:03
2004.08.29
размещение ListView из DLL на форме главного окна


3-1091681441
ydv
2004-08-05 08:50
2004.08.29
Дублирование записей


3-1091444847
Sectey
2004-08-02 15:07
2004.08.29
Расположение бизнес логики приложения.





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский