Pascal/Turbo Pascal - Ayuda pasar numero a binario

 
Vista:
sin imagen de perfil

Ayuda pasar numero a binario

Publicado por cristian (2 intervenciones) el 12/08/2022 18:33:45
Hola nesecito ayuda para crear un procedure para pasar un numero a binario
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var a:real;
procedure binario(a:real);
begin
while a>0 do
begin a:=a/2;
      if a=int(a) then
      begin write('0');
      end
      else
      begin write('1'); a:=a+1
      end;
end;
begin
read(a);
binario;
      /code]
pense en hacer eso, tambien tengo este, pero sirve solo para valores positivos
[code]program DecimalABinario;
uses crt;
var a:longint;
var seguir: char;
 
procedure decimal_a_binario ( numero : longint );
var aux1,aux2 : longint;
var contador, contadorAux: byte;
var V_escritura: array [0..30] of integer;
 
begin
	contador:= 1;
	while numero > 0 do
	begin
		aux1 := numero div 2;
		aux2 := numero mod 2;
 
		numero := aux1;
		V_escritura[contador] := aux2;
 
		contador:=contador+1;
 
	end;
 
	contadorAux := contador-1;
 
	repeat
		begin
			write(V_escritura[contadorAux]);
			contadorAux:= contadorAux-1;
 
		end;
	until contadorAux=0;
 
 
end;
 
begin
repeat
write('Ingrese un numero: ');
readln(a);
write('Su conversion a binario es: ');
decimal_a_binario(a);
writeln();
write('Desea convertir otro numero? s/n: ');
readln(seguir);
until seguir='n';
 
 
readkey();
end.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Ayuda pasar numero a binario

Publicado por joack (4 intervenciones) el 19/10/2022 04:36:47
Una solución sencilla que no requiere de irse por las ramas con tanto codigo y variables es la siguiente:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
program DecimaToBinaryConvertion;
uses
  crt, SysUtils;
 
function decimalToBinary(num:integer): string;
var
  binaryDigit       : string;
  hexRepresentation : string;
begin
  hexRepresentation := '';
 
  repeat
    //Inicializamos el valor de la variable en 0
    binaryDigit := '0';
 
    //Solo si el resto de la division es 1, volvemos a setear
    //la variable pero esta vez con un 1
    if( num MOD 2 = 1 )then
      binaryDigit := '1';
 
    //Concatenamos las dos variables. Prestar atencion a
    //la forma en que concateno el string, esto hace que
    //la representacion binaria quede ordenada al final
    hexRepresentation := binaryDigit + hexRepresentation;
 
    num:= num DIV 2;
  until num = 0;
 
  decimalToBinary:= hexRepresentation;
end;
 
var
  num: integer;
begin
  clrscr;
  write('Ingrese un numero a convertir: ');
  read(num);
 
  writeln('La representacion binaria de ', num, ' es: ', decimalToBinary(num));
end.

Espero que sea lo que andabas buscando, saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar