Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 105,772 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 1,480 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Convert Binary To Hex

Turns Binary String into Hex

Submitted By: AdamSpeight2008
Actions:
Rating:
Views: 154

Language: VB.NET

Last Modified: July 2, 2008

Snippet


  1.   Private Function BinaryToHex(ByRef BitString As String) As String
  2.         ' If the string is empty return an empty string
  3.         If BitString.Length = 0 Then Return ""
  4.         ' If the bitString contains characters that ain't One or Zero, return Empty String
  5.         If System.Text.RegularExpressions.Regex.IsMatch(BitString, "[01]+") = False Then Return ""
  6.         ' Initially make the string empty
  7.         BinaryToHex = ""
  8.         ' define a nibble (4-bits / half a byte)
  9.         Dim nibble As String = ""
  10.         ' Start at the righthandside.
  11.         Dim i As Integer = BitString.Length
  12.         ' while not reached the lefthandside
  13.         While i > 0
  14.             i -= 1
  15.             ' Add the bit a position i to the start of the nibble
  16.             nibble = BitString(i) & nibble
  17.             ' if reach the end of bitstring & the nibble length is less than four.
  18.             If i = 0 And nibble.Length < 4 Then
  19.                 ' Prepend the nibble with the required number of Zeros to make the nibble have length of four.
  20.                 nibble = StrDup(4 - nibble.Length, "0") & nibble
  21.             End If
  22.             ' Find the case which matches the nibble and add the corrisponding hexadecimal representation to
  23.             ' the begining of BinaryToHex String.
  24.             Select Case nibble
  25.                 Case "0000" : BinaryToHex = "0" & BinaryToHex
  26.                 Case "0001" : BinaryToHex = "1" & BinaryToHex
  27.                 Case "0010" : BinaryToHex = "2" & BinaryToHex
  28.                 Case "0011" : BinaryToHex = "3" & BinaryToHex
  29.                 Case "0100" : BinaryToHex = "4" & BinaryToHex
  30.                 Case "0101" : BinaryToHex = "5" & BinaryToHex
  31.                 Case "0110" : BinaryToHex = "6" & BinaryToHex
  32.                 Case "0111" : BinaryToHex = "7" & BinaryToHex
  33.                 Case "1000" : BinaryToHex = "8" & BinaryToHex
  34.                 Case "1001" : BinaryToHex = "9" & BinaryToHex
  35.                 Case "1010" : BinaryToHex = "A" & BinaryToHex
  36.                 Case "1011" : BinaryToHex = "B" & BinaryToHex
  37.                 Case "1100" : BinaryToHex = "C" & BinaryToHex
  38.                 Case "1101" : BinaryToHex = "D" & BinaryToHex
  39.                 Case "1110" : BinaryToHex = "E" & BinaryToHex
  40.                 Case "1111" : BinaryToHex = "F" & BinaryToHex
  41.             End Select
  42.             nibble = ""
  43.         End While
  44.         ' Return the BinaryToHex String
  45.         Return BinaryToHex ' Not required.
  46.     End Function
  47.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month