Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Meta en 4 Julio 2020, 00:11 am



Título: Quiero adaptar este código a otro
Publicado por: Meta en 4 Julio 2020, 00:11 am
Buenas:

Teniendo un código de Python 2.x, quiero adaptarlo a la consola C# si es posible. Si no, pues con Windows Form.
Ya no recuerdo casi nada de Python y quiero tenerlo en C#.

El código del Pyhon es este.
Código
  1. import os, sys, tkFileDialog,Tkinter
  2.  
  3. root = Tkinter.Tk()
  4. root.withdraw()
  5.  
  6. formats = [ ('Roms Super Nintendo SMC','.smc'),('Roms Super Nintendo SFC','.sfc'),('Fichier Bin','.bin'),('Roms Super Nintendo','.smc .sfc .bin') ]
  7.  
  8. input = tkFileDialog.askopenfile(parent=root,mode='rb',filetypes=formats,
  9.                                 title='Seleccione el archivo para intercambiar bin HI a LO como A16->A15, A17->A16...A21->A20 y A15->21')
  10. if not input:
  11.    print "Error: no se puede abrir el archivo."
  12.    sys.exit()
  13.  
  14. output = tkFileDialog.asksaveasfile(parent=root,mode='wb',filetypes=formats,title='Crear nombre de archivo de salida.')
  15. if not output:
  16.    print "Error: no se puede crear el archivo de salida."
  17.    sys.exit()
  18.  
  19.  
  20.    # reading input file to a byte array
  21.    data = bytearray(input.read())
  22.  
  23.    # calculating rom size in 2 exponants
  24.    expsize = 0
  25.    bytesize = len(data)
  26.    while bytesize > 1:
  27.            expsize += 1
  28.            bytesize = bytesize // 2
  29.  
  30.    # init a proper size empty bytearray
  31.    buffer = bytearray()
  32.    for i in range(2**expsize): buffer.append(0)
  33.  
  34.    # let's do the swap
  35.    count = 0
  36.    for i in range(len(data)):
  37.            addr = (i & 0x7fff) + ((i & 0x008000) << (expsize - 16)) + ((i & 0x010000) >> 1) + ((i & 0x020000) >> 1) + ((i & 0x040000) >> 1)
  38.            + ((i & 0x080000) >> 1) + ((i & 0x100000) >> 1) + ((i & 0x200000) >> 1)
  39.            if addr != i: count += 1
  40.            buffer[addr] = data[i]
  41.    print "Intercambiadas %s (%s) direcciones" % (count, hex(count))
  42.  
  43.    # writing output file
  44.    output.write(buffer)
  45.  
  46.    # close file handles
  47.    input.close()
  48.    output.close()

Es cierto que con Visual Studio Community 2019 (https://visualstudio.microsoft.com/es/vs/) me ejecuta bien el Python, pero dependo de compilador y con C# no.

Esto parece una tarea muy complicada.

Muchas gracias.