begin process at 2010 03 22 03:45:24
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths

 > CONVERSION DÉCIMALE/BINAIRE & BINAIRE/DÉCIMALE

CONVERSION DÉCIMALE/BINAIRE & BINAIRE/DÉCIMALE


 Information sur la source

Note :
8,4 / 10 - par 5 personnes
8,40 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Maths Niveau :Débutant Date de création :16/07/2003 Date de mise à jour :16/07/2003 15:42:42 Vu :6 320

Auteur : chnickel

Ecrire un message privé
Commentaire sur cette source (6)
Ajouter un commentaire et/ou une note

 Description

Deux fonctions de conversion Décimale/Binaire & Binaire/Décimale

Source

  • ' CONVERSION D'UNE CHAINE BINAIRE EN VALEUR DECIMALE
  • '
  • ' Binary : du type "100111001" (longeur max 32 caracteres)
  • ' NbBits : nombre de bits a considérer
  • ' Signed : la chaine binaire désigne une valeur décimale signée ou non
  • Public Function BinaryToDecimal(Binary As String, NbBits As Byte, Signed As Boolean) As Long
  • Dim i As Byte
  • BinaryToDecimal = 0
  • If NbBits <= 1 Or NbBits > 32 Then Exit Function
  • If Len(Binary) > NbBits Or Len(Binary) > 32 Then Exit Function
  • Binary = Right("00000000000000000000000000000000" & Binary, NbBits)
  • If Signed = False Then
  • For i = 0 To Len(Binary) - 1
  • BinaryToDecimal = BinaryToDecimal + Val(Mid(Binary, Len(Binary) - i, 1)) * 2 ^ i
  • Next i
  • Else
  • For i = 0 To Len(Binary) - 2
  • BinaryToDecimal = BinaryToDecimal + Val(Mid(Binary, Len(Binary) - i, 1)) * 2 ^ i
  • Next i
  • If Left(Binary, 1) = "1" Then
  • BinaryToDecimal = -((2 ^ (NbBits - 1)) - BinaryToDecimal)
  • End If
  • End If
  • End Function
  • ' CONVERSION D'UNE VALEUR DECIMALE EN CHAINE BINAIRE
  • '
  • ' Decimal : valeur décimale à convertir
  • ' NbBits : nombre de bits a considérer
  • ' Completed : la chaine binaire est complétée de 0 si necéssaire
  • Public Function DecimalToBinary(Decimale As Long, NbBits As Byte, Optional Completed As Boolean) As String
  • Dim L, B As String
  • DecimalToBinary = ""
  • If Decimale > (2 ^ NbBits) - 1 Then Exit Function
  • If Decimale < -(2 ^ (NbBits - 1)) - 1 Then Exit Function
  • If NbBits > 32 Then Exit Function
  • If Decimale < 0 Then
  • Decimale = 2 ^ NbBits + Decimale
  • End If
  • If Decimale = 0 Or Decimale = 1 Then L = "" Else L = 1
  • Do
  • If Decimale Mod 2 = 0 Then B = "0" Else B = "1"
  • DecimalToBinary = DecimalToBinary & B
  • Decimale = Decimale \ 2
  • Loop Until Decimale <= 1
  • DecimalToBinary = StrReverse(DecimalToBinary & L)
  • If Completed = True Then
  • DecimalToBinary = Right("00000000000000000000000000000000" & DecimalToBinary, NbBits)
  • End If
  • End Function
' CONVERSION D'UNE CHAINE BINAIRE EN VALEUR DECIMALE
'
' Binary : du type "100111001" (longeur max 32 caracteres)
' NbBits : nombre de bits a considérer
' Signed : la chaine binaire désigne une valeur décimale signée ou non
Public Function BinaryToDecimal(Binary As String, NbBits As Byte, Signed As Boolean) As Long
    Dim i As Byte
    BinaryToDecimal = 0
    If NbBits <= 1 Or NbBits > 32 Then Exit Function
    If Len(Binary) > NbBits Or Len(Binary) > 32 Then Exit Function
    Binary = Right("00000000000000000000000000000000" & Binary, NbBits)
    If Signed = False Then
        For i = 0 To Len(Binary) - 1
            BinaryToDecimal = BinaryToDecimal + Val(Mid(Binary, Len(Binary) - i, 1)) * 2 ^ i
        Next i
    Else
        For i = 0 To Len(Binary) - 2
            BinaryToDecimal = BinaryToDecimal + Val(Mid(Binary, Len(Binary) - i, 1)) * 2 ^ i
        Next i
        If Left(Binary, 1) = "1" Then
            BinaryToDecimal = -((2 ^ (NbBits - 1)) - BinaryToDecimal)
        End If
    End If
End Function
 
' CONVERSION D'UNE VALEUR DECIMALE EN CHAINE BINAIRE
'
' Decimal : valeur décimale à convertir
' NbBits : nombre de bits a considérer
' Completed : la chaine binaire est complétée de 0 si necéssaire
Public Function DecimalToBinary(Decimale As Long, NbBits As Byte, Optional Completed As Boolean) As String
    Dim L, B As String
    DecimalToBinary = ""
    If Decimale > (2 ^ NbBits) - 1 Then Exit Function
    If Decimale < -(2 ^ (NbBits - 1)) - 1 Then Exit Function
    If NbBits > 32 Then Exit Function
        
    If Decimale < 0 Then
        Decimale = 2 ^ NbBits + Decimale
    End If
    
    If Decimale = 0 Or Decimale = 1 Then L = "" Else L = 1
    Do
        If Decimale Mod 2 = 0 Then B = "0" Else B = "1"
        DecimalToBinary = DecimalToBinary & B
        Decimale = Decimale \ 2
    Loop Until Decimale <= 1
    DecimalToBinary = StrReverse(DecimalToBinary & L)
    If Completed = True Then
        DecimalToBinary = Right("00000000000000000000000000000000" & DecimalToBinary, NbBits)
    End If
End Function



 Sources du même auteur

Source avec Zip [CRYSTAL REPORT] ETAT SANS BDD
SAUVEGARDE / RESTAURATION / MODIFICATION DE LA RÉSOLUTION D'...

 Sources de la même categorie

Source avec Zip Source .NET (Dotnet) COMPILATION A LA VOLÉE, INTERPRÉTER UNE FONCTION MATHÉMATIQU... par sergeb44
Source avec Zip Source .NET (Dotnet) PISH2010-VB2008 par SaintMaur
Source avec Zip Source avec une capture PI-SH-2010-VB6 par SaintMaur
Source avec Zip Source avec une capture CHIFFRAGE ET DECHIFFRAGE FONCTION AFFINE par tresorsdevie
ALGORITHME DE NIVEAU POUR LA RÉSOLUTION DU MÉTHODE POTENTIEL... par sagessekaye

Commentaires et avis

Commentaire de leneuf22 le 17/07/2003 10:27:05

Salut !
Juste histoire de te montrer un code un peu plus simple :

Dim nombre As Long, masque=1 As Long, resultat="" As String, i As Integer
nombre = CLng(InputBox("Nombre ?", "Entre un nombre", ""))

For i = 1 To 31
resultat = CStr(CBool(nombre And masque)*True) & resultat
masque = masque * 2
Next

MsgBox CStr(nombre) & " en décimal équivaut à " & vbCrLf & vbCrLf & resultat & vbCrLf & "en binaire."

Commentaire de sallenou le 26/08/2004 10:02:35

Salut,

leneuf22>Ton code ne marche pas pour des nombres trop élever.

chnickel>Ton code est parfait

Commentaire de acive le 30/11/2004 21:40:10

Salut chnickel
Ca marche super bien... parfait
Merci

Commentaire de DarkSun le 04/04/2005 12:15:53

Niquel...

Petite remarque qui m'a causé des pb : tu modifies les parametres 'Decimale' et 'Binary' transmis à la procedure...

tu devrais plutot en faire une copie et travailler sur la copie...

Commentaire de chnickel le 05/04/2005 11:47:12

Oui tu as raison, il suffit de transmettre les parametres Decimale et Binary en ByVal....

Commentaire de tfares le 19/06/2008 20:07:01

salut leneuf22

VOTRE COE MARCHE POUR LES DECIMAL MAIS POUR LES TYPE DOUBLE ILFAIT UN DEPASSEMENT DE CAPACITE YA T IL SOLUTION

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mars 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,577 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales