Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   de BYTE a STRING (https://www.clubdelphi.com/foros/showthread.php?t=13488)

emeceuy 20-08-2004 05:22:24

de BYTE a STRING
 
Hola...

Tengo un dato guardado en un byte (de 8 bits) que quiero poder pasarlo a tipo cadena o entero, pero entero en base binario (110010001) y NO en base decimal (eso creo que se hace smallint(BYTE_NOM), pero no es lo que quiero). En definitiva quiero tener "visible" que bit está en 0 y que bit está en 1.

Muchas gracias

saludos

mArCe

roman 20-08-2004 06:18:55

¿Algo como esto?

Código Delphi [-]
function BinaryStr(B: Byte): String;
const
  BinChars: array[Boolean] of Char = ('0', '1');

var
  Pot2, I: Integer;

begin
  Pot2 := 1;
  for I := 0 to 7 do
  begin
    Result := BinChars[B and Pot2 <> 0] + Result;
    Pot2 := 2*Pot2;
  end;
end;

// Saludos

roman 20-08-2004 06:38:29

Una versión 'simplificada':

Código Delphi [-]
function BinaryStr(B: Byte): String;
var
  I: Integer;

begin
  for I := 0 to 7 do
  begin
    Result := Chr(Byte(B and 1 <> 0) + Ord('0')) + Result;
    B := B shr 1;
  end;
end;

// Saludos

Mick 20-08-2004 16:29:32

Mas opciones:

Código:

function BinaryStr(B: Byte): String;
var
  I: Integer;
begin
  SetLength(Result,8);
  for I := 1 to 8 do begin
    Result[i]:= PChar('01')[(B SHR (8-I)) AND 1];
  end{for};
end;

Pero sin duda esta es la mas rapida de todas las opciones propuestas:

Código:

function BinaryStr(B: Byte): String;
var
  I  : Integer;
  Ptr: PChar;
begin
  SetLength(Result,8);
  Ptr:= PChar(Result);
  for I:= 7 downto 0 do begin
    Ptr^:= PChar('01')[(B SHR I) AND 1];
    Inc(Ptr);
  end{for};
end;:

Saludos


La franja horaria es GMT +2. Ahora son las 16:37:39.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi