Search code examples
c#binaryreader

how to convert byte array to string in binary reader in c#?


i have this code

static void Main(string[] args)
    {
        BinaryWriter bw = new BinaryWriter(File.Open("mama.bin", FileMode.Truncate));

        bw.Write("Hello world");




        bw.Flush();
        bw.Close();
        BinaryReader br = new BinaryReader(File.Open("mama.bin", FileMode.OpenOrCreate));
        byte[] data = br.ReadBytes(8);
        string x = BitConverter.ToString(data);
        Console.WriteLine(x);




        Console.ReadKey();
    }

in the output i get this -> 0B-48-65-6C-6C-6F-20-77

how can i convert byte[] to my correct string ?? and i think this is a string encoding ?

can any one help me

Thanks :)


Solution

  • try

    string x = System.Text.Encoding.UTF8.GetString(data);