none
Write data hex into file RRS feed

  • Question

  •  

    Hi,

    I have a big problem:

     

    I use c# language in framwork 2.0 for write an binary data into .txt file:

       

     

        int32 MyValues = 270;

        FileStream fs = new FileStream(strOutputFilename, FileMode.Append);
        BinaryWriter w = new BinaryWriter(fs);

        w.write(myValue);

     

    The result for this function into output file is:

     

    0E 01 00 00

     

    What are I do if I want write 00 00 01 0E?

     

     

     

     

    Wednesday, August 1, 2007 4:28 PM

Answers

  • Here's that, along with methods to write Int64s and Int16s in big-endian byte order.

     

    Code Snippet

    private static void writeBigEndian(BinaryWriter bw, Int64 value)
    {
        WriteBigEndian(bw, (Int32)((value >> 32) & 0xFFFFFFFFL));
        WriteBigEndian(bw, (Int32)((value)       & 0xFFFFFFFFL));
    }

     

    private static void writeBigEndian(BinaryWriter bw, Int32 value)
    {
        bw.Write(new byte[]
            {
                (byte)((value >> 24) & 0xFF),
                (byte)((value >> 16) & 0xFF),
                (byte)((value >> 8)  & 0xFF),
                (byte)((value)       & 0xFF)
            });
    }

     

    private static void writeBigEndian(BinaryWriter bw, Int16 value)
    {
        bw.Write(new byte[]
            {
                (byte)((value >> 8)  & 0xFF),
                (byte)((value)       & 0xFF)
            });
    }

     

     

    Use it like

    Code Snippet

    writeBigEndian(w, myValue);

    Wednesday, August 1, 2007 5:01 PM
  • m.primius,

    there is an old hack to do this, using the IPAddress.HostToNetworkOrder, which handles 16, 32, and 64 bits integers.

     

    ...

    w.Write (System.Net.IPAddress.HostToNetworkOrder (myValue));

     

    while this works, I would not recommend it for production code as the method is not very self-documenting.

    If you plan to write a lot of data to big endian, you might consider rolling your own derived BinaryWriter. It is not a lot of work: all you need to do is to override some of the Write methods, and they will all contain the same code:

     

    public virtual void Write (int value) {

      byte [] temp = BitConverter.GetBytes (value);

      Array.Reverse (temp);

      base.Write (temp, 0, temp.Length);

    }

     

    If you have a taste for overkill, you can even write:

     

    public virtual void Write (int value) {

      if (BitConverter.IsLittleEndian) {

        byte [] temp = BitConverter.GetBytes (value);

        Array.Reverse (temp);

        base.Write (temp, 0, temp.Length);

      } else {

        base.Write (value);

      }

    }

     

    HTH

    --mc

    Wednesday, August 1, 2007 5:20 PM

All replies

  • Here's that, along with methods to write Int64s and Int16s in big-endian byte order.

     

    Code Snippet

    private static void writeBigEndian(BinaryWriter bw, Int64 value)
    {
        WriteBigEndian(bw, (Int32)((value >> 32) & 0xFFFFFFFFL));
        WriteBigEndian(bw, (Int32)((value)       & 0xFFFFFFFFL));
    }

     

    private static void writeBigEndian(BinaryWriter bw, Int32 value)
    {
        bw.Write(new byte[]
            {
                (byte)((value >> 24) & 0xFF),
                (byte)((value >> 16) & 0xFF),
                (byte)((value >> 8)  & 0xFF),
                (byte)((value)       & 0xFF)
            });
    }

     

    private static void writeBigEndian(BinaryWriter bw, Int16 value)
    {
        bw.Write(new byte[]
            {
                (byte)((value >> 8)  & 0xFF),
                (byte)((value)       & 0xFF)
            });
    }

     

     

    Use it like

    Code Snippet

    writeBigEndian(w, myValue);

    Wednesday, August 1, 2007 5:01 PM
  • m.primius,

    there is an old hack to do this, using the IPAddress.HostToNetworkOrder, which handles 16, 32, and 64 bits integers.

     

    ...

    w.Write (System.Net.IPAddress.HostToNetworkOrder (myValue));

     

    while this works, I would not recommend it for production code as the method is not very self-documenting.

    If you plan to write a lot of data to big endian, you might consider rolling your own derived BinaryWriter. It is not a lot of work: all you need to do is to override some of the Write methods, and they will all contain the same code:

     

    public virtual void Write (int value) {

      byte [] temp = BitConverter.GetBytes (value);

      Array.Reverse (temp);

      base.Write (temp, 0, temp.Length);

    }

     

    If you have a taste for overkill, you can even write:

     

    public virtual void Write (int value) {

      if (BitConverter.IsLittleEndian) {

        byte [] temp = BitConverter.GetBytes (value);

        Array.Reverse (temp);

        base.Write (temp, 0, temp.Length);

      } else {

        base.Write (value);

      }

    }

     

    HTH

    --mc

    Wednesday, August 1, 2007 5:20 PM
  • Here's the source for those HostToNetworkOrder methods, if you're interested:

     

    public static short HostToNetworkOrder(short host)
    {
        return (short) (((host & 0xff) << 0x8) | ((host >> 0x8) & 0xff));
    }
     
    public static int HostToNetworkOrder(int host)
    {
        return (((HostToNetworkOrder((short) host) & 0xffff) << 0x10) | (HostToNetworkOrder((short) (host >> 0x10)) & 0xffff));
    }
    
     
    public static long HostToNetworkOrder(long host)
    {
        return (long) (((HostToNetworkOrder((int) host) & 0xffffffff) << 0x20) | (HostToNetworkOrder((int) (host >> 0x20)) & 0xffffffff));
    }
    

    Wednesday, August 1, 2007 8:39 PM
  •  

    WORKS!!!!!!!!!!!!!!!!!!

     

     

    I love you!!!!!!!!

     

    Thanks a lot!!!!!!!!

    Thursday, August 2, 2007 8:55 AM