-1

I need help from String to Binary in C#. I have tried to make it like '01100100', but it comes out like 01100100', without the apostrophe on the left side. In my code it says pad left(8, '0') and I can't seem to get it like (1, " ' ") or something.

private void Button2_Click_1(object sender, EventArgs e)
{
    richTextbox1.Text = ToBinary(ConvertToByteArray(richTextbox1.Text, Encoding.ASCII));
}
public static byte[] ConvertToByteArray(string str, Encoding encoding)
{
    return encoding.GetBytes(str);    
}

public static String ToBinary(Byte[] data)
{
    return string.Join("', ", data.Select(byt => Convert.ToString(byt, 2).PadLeft(8,'0')));   
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
  • 1
    It looks to me like you're inserting `',` between the strings in `ToBinary`, but you're not adding the quote before or at the end. `PadLeft` is being applied beforehand—all it is doing is making sure every number is 8 digits long by adding zeros to the left end. – jirassimok Sep 21 '19 at 14:29
  • Im new to coding that kinda hurts my brain but i think i know what you're saying a bit. So, you're saying i can apply it after hand ? – Dumb Coder Sep 21 '19 at 14:32
  • What is input and what output you are expecting? show us relative code instead of full functionality. `Convert.ToString(byt, 2)` will be compile time error – Prasad Telkikar Sep 21 '19 at 14:49
  • i am trying to put a `script` into a textbox then it would obfuscate or turn in to `Binary` code – Dumb Coder Sep 21 '19 at 14:56

2 Answers2

1

It looks like you just need to add the leading (and trailing) quotes outside of the call to Join. I don't know C#, so I can't tell you how to do that, but string concatenation is a common operation, so it should be easy enough to find.

jirassimok
  • 3,850
  • 2
  • 14
  • 23
0

Why not use this?

return "'" + data.Select(byt => Convert.ToString(byt, 2).PadLeft(8,'0') + "',";
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52