Accueil > > > CONVERSION DÉCIMALE/BINAIRE & BINAIRE/DÉCIMALE
CONVERSION DÉCIMALE/BINAIRE & BINAIRE/DÉCIMALE
Information sur la source
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
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|