0

I have a byte array:

newMsg.DATA = new byte[64];

How can I convert it into binary value and then write it in text file with comma separation. Comma should be in between binary values not bytes.....

like 1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0.......

3
  • 5
    There are several steps here: 1) Extracting individual bits from a byte; 2) Converting bits to strings; 3) Creating a comma-separated representation; 4) Writing that text to a file. Please show what you've tried so far - at the moment there's no sign that you've tried anything, which makes it less likely that others will help you. Commented Mar 5, 2018 at 8:40
  • 1
    every subquestion in this post has been asked here multiple times. Please put some effort in research first., Then please post the code that you have and explain where you got stuck. Good luck Commented Mar 5, 2018 at 8:40
  • 1
    So what should be between the bytes then? Because your example does have commas between the values beyond 8 bits. Commented Mar 5, 2018 at 11:21

3 Answers 3

2

Here is an example that uses LINQ:

byte[] arr = new byte[] { 11, 55, 255, 188, 99, 22, 31, 43, 25, 122 };

string[] result = arr.Select(x => string.Join(",", Convert.ToString(x, 2)
                     .PadLeft(8, '0').ToCharArray())).ToArray();

System.IO.File.WriteAllLines(@"D:\myFile.txt", result);

Every number in byte[] arr is converted to a binary number with Convert.ToString(x, 2) and the comma "," is added between binary values with string.Join(",",...). At the end you can write all the elements in result to a text file by using System.IO.File.WriteAllLines.

The example above gives you this kind of output in a txt file:

0,0,0,0,1,0,1,1
0,0,1,1,0,1,1,1
1,1,1,1,1,1,1,1
...

Explanation of Convert.ToString(value, baseValue):

The first parameter value represents the number you want to convert to a string and the second parameter baseValue represents which type of conversion you want to perform.

Posible baseValues are : 2,8,10 and 16.

BaseValue = 2 - represents a conversion to a binary number representation.

BaseValue = 8 - represents a conversion to a octal number representation.

BaseValue = 10 - represents a conversion to a decimal number representation.

BaseValue = 16 - represents a conversion to a hexadecimal number representation.

Sign up to request clarification or add additional context in comments.

3 Comments

the only answer with a decent explanation. Have an upvote. May be you could explain the meaning of the 2 in Convert.ToString(x, 2)
@MongZhu are you pleased with the explanation
amazed ;) more than thorough! I would upvote once more if I could :)
1

I think this will Help you c# provides inbuilt functionality to do so

with help of Convert.ToString(byte[],base); here base could be[2(binary),8(octal),16(HexaDecimal)]

        byte[] data = new byte[64];
        // 2nd parameter 2 is Base e.g.(binary)
        string a = Convert.ToString(data[data.Length], 2);
        StringBuilder sb = new StringBuilder();
        foreach(char ch in a.ToCharArray())
        {
            sb.Append(ch+",");
        }
        // This is to remove last extra ,
        string ans = sb.ToString().Remove(sb.Length - 1, 1);

2 Comments

it might be confusing if you don't explain why you chose to convert only the byte at position 20. Also a little bit more explanation would really improve your answer. like: ToString(data[20], 2); what is the meaning of the 2 ?=! :)
@MongZhu Thanks for suggestions. did edit answer according to that.
0

This should get you going:

var bytes = new byte[] { 128, 255, 2 };
var stringBuilder = new StringBuilder();
for (var index = 0; index < bytes.Length; index++)
{
    var binary = Convert.ToString(bytes[index], 2).PadLeft(8, '0');
    var str = string.Join(",", binary.ToCharArray());
    stringBuilder.Append(str);
    if (index != bytes.Length -1) stringBuilder.Append(",");
}
Console.WriteLine(stringBuilder);

1 Comment

This line var str = string.Join(",", binary.ToCharArray()); is giving error......its saying best overload method is string.join(string,string[])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.