byte value write using binarywrite.write is not correct?

映雪 王 21 Reputation points
2021-03-31T09:14:15.9+00:00
                    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...

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,286 questions
{count} votes

Accepted answer
  1. WayneAKing 4,921 Reputation points
    2021-03-31T20:42:05.423+00:00

    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

0 additional answers

Sort by: Most helpful