Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Dúvidas sobre cálculos, funções simples e aninhadas, fórmulas matriciais, etc.
Por Cidnei
#9786
Gostaria de saber como proceder para converter, na função DECABIN, um 'núm' maior que 511 e menor que -512, aumentar [casas], apenas como exemplo: converter o decimal 1962(base 10) para binário 11110101010(base 2) e o inverso binário para decimal, ou seja BINADEC, gostaria de trabalhar com estas conversões no Excel, é possível?
Na "Ajuda do Excel" só diz que: - Se núm < -512 ou se núm > 511, DECABIN retornará o valor de erro #NÚM!, o que comprovei!
Obs.: Tenho Excel 2013 e Win 7 Ultimate.
Forte abraço,
Cidnei C. Alencar
#9848
A função DECABIN() tem essa limitação mesmo segundo o Help.

Uma alternativa é você mesmo construir um algoritmo para isso. Faz tanto tempo que aprendi isso na faculdade que não lembro mais...

Achei algo interessante no próprio site da Microsoft. Acho que vale a lida e tentar entender pra ver se se aplica.

Daí é só transformar numa UDF, fazer um teste de mesa comparando o resultado com o intervalo de aceitação da função DECABIN() e usar após comprovação que o algoritmo faz sentido.

Fonte: https://support.microsoft.com/en-us/kb/109260
Código: Selecionar todos
 Sub Text1_Change ()

      Dim i As Long, x As Long, bin As String
      Const maxpower = 30   ' Maximum number of binary digits supported.
      text1.MaxLength = 9   ' Maximum number of decimal digits allowed.
      text2.Enabled = False ' Prevent typing in second text box.
      bin = ""  'Build the desired binary number in this string, bin.
      x = Val(text1.Text) 'Convert decimal string in text1 to long integer

      If x > 2 ^ maxpower Then
         MsgBox "Number must be no larger than " & Str$(2 ^ maxpower)
         text2.Text = ""
         Exit Sub
      End If

      ' Here is the heart of the conversion from decimal to binary:

      ' Negative numbers have "1" in the 32nd left-most digit:
      If x < 0 Then bin = bin + "1" Else bin = bin + "0"

      For i = maxpower To 0 Step -1
         If x And (2 ^ i) Then   ' Use the logical "AND" operator.
            bin = bin + "1"
         Else
            bin = bin + "0"
         End If
      Next
      text2.Text = bin  ' The bin string contains the binary number.

   End Sub
Abs.
Por Cidnei
#9954
Olá orosolin2, muito obrigado pelo apoio e presteza.
O programa funciona perfeitamente e é ótimo!
Estou tentando invertê-lo para BINADEC, ou seja, binário para decimal, que também preciso, conforme mencionei em minha pergunta acima, mas meu conhecimento em VBA é muito pouco!
Teria uma sugestão?
Abraço,
Cidnei C. Alencar
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord