Little Endian & Binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • UnknownBlue
    New Member
    • Jan 2007
    • 14

    #1

    Little Endian & Binary

    Hi.

    How do I display a binary data that is formatted in little endian in Visual C++ 6?
    Can I just display it as per normal or is there an additional code to use to read and display it?
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by UnknownBlue
    Hi.

    How do I display a binary data that is formatted in little endian in Visual C++ 6?
    Can I just display it as per normal or is there an additional code to use to read and display it?
    What are you trying to do? You can display binary data that is formatted in little or big endian using a normal hex editor or binary reader like TextPad. If you are trying to read in serialized data that is in little endian, you should be able to just use the standard binary serialization library to read in that data.

    In other words, the short answer to "How do I display binary data in little endian?" is "by double clicking on it". The long answer requires more specification on your part.

    Comment

    • UnknownBlue
      New Member
      • Jan 2007
      • 14

      #3
      The hex display of the data is like this: 2E FA CC F6 9F FC... So since it is little endian, do I have to change the data to FA 2E F6 CC FC 9F?
      And Im supposed to divide the data by two. Do I just divide 2E by two or FA 2E by two?

      I just cant figure this out. Can someone help me?

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by UnknownBlue
        The hex display of the data is like this: 2E FA CC F6 9F FC... So since it is little endian, do I have to change the data to FA 2E F6 CC FC 9F?
        And Im supposed to divide the data by two. Do I just divide 2E by two or FA 2E by two?

        I just cant figure this out. Can someone help me?
        Again, I don't know how to help you unless you can be more specific about your problem. Yes its true that little endian means that the data is stored "little end first" but if you want to display it in a hex editor just open your favorite hex editor and then open the file, its as simple as that. If you are trying to read in the binary data into an application then you could open a file stream and read in some bytes, then swap the low order bits with the hi order ones. Or you could use a library to open a binary stream reader and get it that way. Or you could send the raw data to a printer because after all that is a display device.

        You need to be more specific.

        Comment

        • UnknownBlue
          New Member
          • Jan 2007
          • 14

          #5
          Hi. Sorry for not being specific. Im trying to do the second one. The one on swapping the low order bytes with the high order ones. Im using VC6.
          Code:
          #include <iostream>
          #include <fstream>
          using namespace std;
          
          ifstream file;
          ofstream newFile;
          
          int main ()
          {
          	int j = 0, i;
          	char array[10], chg[5];
          
          	file.open ("s0455_re.dat", ios::in|ios::binary);
          	newFile.open ("NewFile.bin", ios::out|ios::binary);
          
          	if (file.is_open())
          	{
          		cout << "Able to open file!" << endl;
          
          		if (newFile.is_open())
          		{
          			cout << "Able to open new file!" << endl;
          			
          			while (j < 10)	// loop while extraction from file is possible
          			{	
          				array[j] = file.get();       // get character from file
          				j++;
          			}
          
          			j = 0;
          
          			for (i=0; i<5; i++)		// i is half of j
          			{
          				chg[i] = ((int) array[j+1] << 8) + array[j];		// swap position
          				newFile << chg[i];
          				
          				j = j + 2;
          			}
          			newFile.close();
          		}
          
          		else
          			cout << "Unable to open new file.";
          
          		file.close();
          	}
          	else
          		cout << "Unable to open file";
          
          	return 0;
          }
          In the swap position, Im trying to swap the position of the first byte with the second one, the third with the forth and so on. But when i write on the newFile, its only the first, third etc bytes are written. What should i do for the swap position?
          Thanks for your patience.

          Comment

          Working...