Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone.
I can not convert this code to vb.net.
This is my function :
C#
public static string StringToBinary(string data)
{
 StringBuilder sb = new StringBuilder();
 
 foreach (char c in data.ToCharArray())
 {
 sb.Append(Convert.ToString(c, 2).PadLeft(8,'0'));
 }
 return sb.ToString();
}


But I Can't Convert this part :
C#
sb.Append(Convert.ToString(c, 2).PadLeft(8,'0'));


Thank you.
Posted

Try:
VB
Public Shared Function StringToBinary(data As String) As String
    Dim sb As New StringBuilder()

    For Each c As Char In data.ToCharArray()
        sb.Append(Convert.ToString(c, 2).PadLeft(8, "0"C))
    Next
    Return sb.ToString()
End Function

If you have a look, there are online tools to do this:
http://www.developerfusion.com/tools/convert/csharp-to-vb/[^]
And
http://converter.telerik.com/[^]
For example. I used the Telrik convert for this.
 
Share this answer
 
You can use this code ....

VB
Public Shared Function StringToBinary(ByVal data As String) As String
 Dim sb As StringBuilder =  New StringBuilder()

 Dim c As Char
 For Each c In data.ToCharArray()
 sb.Append(Convert.ToString(c, 2).PadLeft(8,"0"c))
 Next
 Return sb.ToString()
End Function
 
Share this answer
 
v2
Comments
Sina asefi 5-Jan-15 9:16am    
Sir first thank you for your response.
But it has problem in this line :
sb.Append(Convert.ToString(c, 2).PadLeft(8,"0"c))
If you check it in visual studio , you could see it.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900