question

44651503 avatar image
0 Votes"
44651503 asked 44651503 commented

byte value write using binarywrite.write is not correct?

                     byte[] BTFWCorrectlogArr = BTFWlogCollect.ToArray();
                    for(int i=0;i<BTFWCorrectlogArr.Length;i++)
                    {
                         Console.WriteLine("{0}", BTFWCorrectlogArr[i]);
                         bw_btfwlogging.Write(BTFWCorrectlogArr[i]);
                         Console.WriteLine("{0}", BTFWCorrectlogArr[i]);
                     }

The print using console is correct(0xA0), but the value write to .bin file is not correct(0xD0 0xB5).
Sometimes value write to .bin file is correct and sometimes not, I don't know why. I‘ve tried everything, such as encoding, BitConverter.GetBytes, Convert.ToByte...

dotnet-csharp
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


What bw_btfwlogging is and what happen if you write to file just a single 0xA0 byte without any loop?

0 Votes 0 ·
  1. this is bw_btfwlogging :

               /* create a file to store BT FW log*/
                 DateTime dt = DateTime.Now;
                 string filepath = Directory.GetCurrentDirectory() + "\\log\\" + string.Format("BT_FW_{0}_{1:D2}_{2:D2}_{3:D2}{4:D2}{5:D2}.bin", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
    
                 fs_btfwlogging = new FileStream(filepath, FileMode.Create);
                 bw_btfwlogging = new BinaryWriter(fs_btfwlogging);
    

  2. well, a single 0xA0 byte works, the context saved in .bin file is 0xA0!


0 Votes 0 ·

Try removing bw_btfwlogging and use fs_btfwlogging.WriteByte or fs_btfwlogging.Write.

There is a constructor of BinaryWriter that takes an Encoding parameter, but it is not clear if you need it.

0 Votes 0 ·
Show more comments

In addition to what @Viorel-1 said is it possible to write the array at one time? if is it does it write correctly.

0 Votes 0 ·

an array at one time doesn't work for me :(
like this: bw_btfwlogging.Write(BTFWCorrectlogArr); it turns out that all value larger than 0x7F is not correct, smaller than 0x7F is correct

0 Votes 0 ·

1 Answer

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered 44651503 commented

The print using console is correct(0xA0),
but the value write to .bin file is not correct(0xD0 0xB5).

What are you saying? The console shows one byte but you
claim the file has two bytes?

When input: 0xA0~0xB6, it's ok.
When input is 0xA5, 0xA5, 0x00, 0x33~0x3A, 0xA5, 0xA5...
value more than 0x7F is wrong again

You say 0xB6 is OK, yet you claim that values more
than 0x7F are wrong. But 0xB6 is greater than 0x7F.

You're not providing enough information re what you are
doing and how you are doing it.

Since the Console.WriteLine is displaying characters as
decimal values, how are you arriving at the hex values
you are describing?

How are you checking the contents of the output file?
What tool are you using?

As an aside, I trust you know that the code you have
won't automatically create a subdir named "log". If
one doesn't exist you will get a run time exception.

Build and run this code.

 static void Main(string[] args)
 {
     FileStream fs_btfwlogging;
     BinaryWriter bw_btfwlogging;
    
     /* create a file to store BT FW log*/
     DateTime dt = DateTime.Now;            
     string filepath = Directory.GetCurrentDirectory() + "\\log\\" 
         + string.Format("BT_FW_{0}_{1:D2}_{2:D2}_{3:D2}{4:D2}{5:D2}.bin", 
         dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
    
     using (fs_btfwlogging = new FileStream(filepath, FileMode.Create))
     {                
         bw_btfwlogging = new BinaryWriter(fs_btfwlogging);
    
         //byte[] BTFWCorrectlogArr = BTFWlogCollect.ToArray();
         byte[] BTFWCorrectlogArr = 
             { 0x0A, 0x0D, 0xA0, 0xD0, 0x7F, 0x8F, 0xF7, 0xFF };
    
         for (int i = 0; i < BTFWCorrectlogArr.Length; i++)
         {
             //Console.WriteLine("{0}", BTFWCorrectlogArr[i]);
             Console.WriteLine("{0:X2}", BTFWCorrectlogArr[i]);
             bw_btfwlogging.Write(BTFWCorrectlogArr[i]);
         }
     }
 }

The console should show:

0A
0D
A0
D0
7F
8F
F7
FF

The file will contain this when viewed using a hex
viewer/editor:

83435-file-out.jpg

If your results differ you must be doing something wrong
outside of the code itself.

  • Wayne



file-out.jpg (11.7 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I think I find the reason, the same .bin file shows different when I use another tool. Although I don't know the reason, the problem is solved! Thanks! 83463-file.png


0 Votes 0 ·
file.png (66.2 KiB)