0

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 :)

6
  • 1
    Dont use A BinaryWriter - Reader. Use StreamWriter - Reader instead Commented Oct 22, 2019 at 14:32
  • or use ReadString (as you used Write(string)) Commented Oct 22, 2019 at 14:33
  • 1
    Yeah i know i can use the ReadString or stream writer but i have question , can i convert bytes to string and get correct value like in double :) Commented Oct 22, 2019 at 14:39
  • Try following : byte[] input = { 0x0B, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77 }; string output = Encoding.ASCII.GetString(input); Commented Oct 22, 2019 at 14:42
  • @jdweng ohh thanks it is working but in below answer that innat3 wrote he used utf8 and you used ASCII are there any difference or it is the same thing :) Commented Oct 22, 2019 at 14:52

1 Answer 1

1

try

string x = System.Text.Encoding.UTF8.GetString(data);
Sign up to request clarification or add additional context in comments.

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.