Results 1 to 4 of 4

Thread: Byte Order Question - How to reverse?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    Byte Order Question - How to reverse?

    I'm creating a binary file using the binarywriter class. I'm writing decimal numbers to the file, each one as a 4 byte representation.

    So, if I wanted to write the decimal number 10523

    The hex would be 0x291B.

    I need the bytes written to the file in this order:

    0x1B 0x29 0x00 0x00

    My problem is that using this code:

    Code:
                byte[] numBuffer = null;
                decvalue = 10523;
                numBuffer = System.Text.Encoding.ASCII.GetBytes(decvalue.ToString("x4"));
                wrtBin.Write(numBuffer);
    The bytes are written to the file in this order:

    0x29 0x1b 0x00 0x00

    What's the best way for me to do this?
    Brandon S Davids

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Byte Order Question - How to reverse?

    What is the logic you have for this reversal? What if it were a larger number like 0x23bdc5? Would you still just reverse the first two bytes?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2002
    Posts
    196

    Re: Byte Order Question - How to reverse?

    0x23bdc5 should be:

    0xC5 0xBD 0x23 0x00

    Also... He says that if I use my c# code and do this:

    Code:
    WriteFile( hf, ( BYTE *)&dwSomeDwordValue, sizeof( DWORD ), ... );
    That the bytes will be written in the correct order. I have no clue how to do that in c# though.
    Brandon S Davids

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Byte Order Question - How to reverse?

    Alright, to reverse the array, you can use Array.Reverse(arrayname).

    About WriteFile, what language is 'he' talking about? Here is .NET's WriteFile,

    http://msdn.microsoft.com/library/de...efiletopic.asp

    Maybe you're looking for one of these.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width