Posts Tagged ‘conversion

28
Mar
09

[VB.Net] Binary To String Conversion

    Public Function BinaryToString(ByVal Binary As String) As String
        Dim Characters As String = System.Text.RegularExpressions.Regex.Replace(Binary, "[^01]", "")
        Dim ByteArray((Characters.Length / 8 ) - 1) As Byte
        For Index As Integer = 0 To ByteArray.Length - 1
            ByteArray(Index) = Convert.ToByte(Characters.Substring(Index * 8, 8), 2)
        Next
        Return System.Text.ASCIIEncoding.ASCII.GetString(ByteArray)
    End Function

Usage:

Debug.WriteLine(BinaryToString("01110011 01101001 01101101 00110000 01101110"))

Would output:

sim0n

Debug.WriteLine(BinaryToString("01110011,01101001,01101101,00110000,01101110,"))

Would output:

sim0n

Advertisements
28
Mar
09

[VB.Net] String To Binary Conversion

    Public Function StringToBinary(ByVal Text As String, Optional ByVal Separator As String = " ") As String
        Dim oReturn As New System.Text.StringBuilder
        For Each Character As Byte In System.Text.ASCIIEncoding.ASCII.GetBytes(Text)
            oReturn.Append(Convert.ToString(Character, 2).PadLeft(8, "0"))
            oReturn.Append(Separator)
        Next
        Return oReturn.ToString
    End Function

Usage:

Debug.WriteLine(StringToBinary("sim0n")

Would output:
01110011 01101001 01101101 00110000 01101110

Debug.WriteLine(StringToBinary("sim0n", ","))

Would output:
01110011,01101001,01101101,00110000,01101110,

28
Mar
09

[VB.Net] Hex to String Conversion

Function HexToString(ByVal hex As String) As String
    Dim text As New System.Text.StringBuilder(hex.Length \ 2)
    For i As Integer = 0 To hex.Length - 2 Step 2
        text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
    Next
    Return text.ToString
End Function

Usage:

Debug.WriteLine(HexToString("73696D306E"))

Would output:
sim0n

28
Mar
09

[VB.Net] String to Hex Conversion

Migrating old code snippits over to the new blog.

Function StringToHex(ByVal text As String) As String
    Dim hex As String
    For i As Integer = 0 To text.Length - 1
        hex &= Asc(text.Substring(i, 1)).ToString("x").ToUpper
    Next
    Return hex
End Function

Usage:

Debug.WriteLine(StringToHex("sim0n"))

Would Output:
73696D306E

27
Mar
09

[ActionScript] Integer To Binary Algorithm

Ahhha so, since ive been studying a few algorithms recently, I thought I would code one to test it out. This uses a basic loop to convert a number to binary:

function integerToBinary(number:Number):String {
	var binary:String = "";
	while (number != 0) {
		if (number%2 == 0) {
			binary = "0"+binary;
		} else {
			binary = "1"+binary;
			number--;
		}
		number = number/2;
	}
	return binary;
}

Usage:

trace(_root.integerToBinary(123));

Would output:

1111011