Base conversion

Asked

Viewed 89 times

2

In a college job we were required to simulate the functioning of a RAM memory. The user can read or write in the cells of this memory or all Zera, and each one stores 8 bits. That part’s already done, but there’s something I can’t fix. The user can ask for the address of a cell or read its contents in binary or hexadecimal according to his choice, but I’m having difficulty converting the elements of each cell (which are already in binary) to hexadecimal.

The code is like this:

import numpy as np

celulas = np.array([(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0)])
while True:
    operação = str(input('Ler[A], Escrever[B] ou Limpar[c]? Para terminar a aplicação[D]')).upper()
    if operação == 'A':
        endereço = int(input('Qual célula ler?'))
        base = int((input('Você quer ler em binário(1) ou em hexadecimal(2)?')))
        if base == 1:
            cels = [str(i) for i in celulas[endereço - 1]]
            print(''.join(cels))
    if operação == 'B':
        endereço = int(input('Qual célula escrever?'))
        cont = 0
        while cont <= 7:
            celulas[endereço-1][cont] = int(input('Digite 1 ou 0'))
            cont += 1
    if operação == 'C':
        cont = 0
        while cont <= 31:
            celulas[cont] = (0,0,0,0,0,0,0,0)
            cont += 1
    if operação == 'D':
        break

From now on, thank you.

1 answer

0


The easiest way to obtain a hexadecimal representation is to transform the value in question into bytes and call its function .hex().

Since you are not dealing in your "memory" with really binary numbers, but integers that assume the value 1 or 0, we have to

  1. Turn integers into a binary string
  2. Transform the binary string into an integer
  3. Turn the whole into bytes
  4. Apply the function hex

We can do it like this:

base = int((input('Você quer ler em binário(1) ou em hexadecimal(2)?')))
if base == 1:
    cels = [str(i) for i in celulas[endereço - 1]]
    print(''.join(cels))
if base == 2:

    # Pegar a representação binária da célula (10111001)
    cels = [str(i) for i in celulas[endereço - 1]]
    representacao_binaria = ''.join(cels)

    # Converter representação binária em inteiro (indicando que nossa
    # string está em base 2, binária)
    inteiro = int(representacao_binaria, base=2)

    # Transformamos o inteiro em um byte, e pegamos sua representação
    # hexadecimal (ex. b9)
    representacao_hexadecimal = inteiro.to_bytes(1, sys.byteorder).hex()

    # Mudamos um pouco a apresentação (b9 -> 0xB9)
    print('0x' + representacao_hexadecimal.upper())
  • 1

    Perfect, my friend, it worked. Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.