1

I have a byte array and I would like to get values from it into variables. I know the values format like string, unsigned int etc.

byte[] buffer = File.ReadAllBytes("binarydata.bin");
string value1 = ???
uint16 value2 = ???
string value3 = ???
uint32 value4 = ???

How can i assign the values? I know that the first value is a string of 8, i know the second value is a usigned 16bit int, and the third value is a string of 12 and the forth is a unsigned 32 bit int.

4
  • 1
    BitConverter class Commented Jun 1, 2018 at 7:43
  • Use BinaryReader or BitConverter. Commented Jun 1, 2018 at 7:44
  • How was this file created? This is important to know, because the string value will need some metadata to be present in the file to indicate the length of the string. (Usually length prefixed, but who knows?) Commented Jun 1, 2018 at 8:19
  • Using BitConverter and ToString i get the value as hexadecimal instead of human readable text, how can I get it so its readable? Commented Jun 1, 2018 at 9:00

2 Answers 2

3

You could simply use BinaryReader

Reads primitive data types as binary values in a specific encoding

Example

using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
    var aspectRatio = reader.ReadSingle();
    var tempDirectory = reader.ReadString();
    var autoSaveTime = reader.ReadInt32();
    var showStatusBar = reader.ReadBoolean();

    Console.WriteLine("Aspect ratio set to: " + aspectRatio);
    Console.WriteLine("Temp directory is: " + tempDirectory);
    Console.WriteLine("Auto save time set to: " + autoSaveTime);
    Console.WriteLine("Show status bar: " + showStatusBar);
}

Update from xanatos

ReadString Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.

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

1 Comment

Put a note on the format the ReadString() expects. I don't think anyone else codifies strings in the same way.
0

You can use BinaryReader ,this example writes data as binary. After reads binary data and assigns to variables.

using System;
using System.IO;

class ConsoleApplication
{
const string fileName = "AppSettings.dat";

static void Main()
{
    WriteDefaultValues();
    DisplayValues();
}

public static void WriteDefaultValues()
{
    using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
    {
        writer.Write(1.250F);
        writer.Write(@"c:\Temp");
        writer.Write(10);
        writer.Write(true);
    }
}

public static void DisplayValues()
{
    float aspectRatio;
    string tempDirectory;
    int autoSaveTime;
    bool showStatusBar;

    if (File.Exists(fileName))
    {
        using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
        {
            aspectRatio = reader.ReadSingle();
            tempDirectory = reader.ReadString();
            autoSaveTime = reader.ReadInt32();
            showStatusBar = reader.ReadBoolean();
        }

        Console.WriteLine("Aspect ratio set to: " + aspectRatio);
        Console.WriteLine("Temp directory is: " + tempDirectory);
        Console.WriteLine("Auto save time set to: " + autoSaveTime);
        Console.WriteLine("Show status bar: " + showStatusBar);
        }
    }
}

Comments

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.