Byteorder messed up.

Posted on 2004-09-24
Last Modified: 2010-04-01
I made code which should output the following:
but it outputs this:
If you look wel,
you see that they are the same, but that the byteorder is just messed up
so, generally speaking:
I think it has something to do with most significant byte and ....
Is there a "standart solution" for my problem (like defining "which byteorder" you want or something else....)???

Thanks in advance.
Question by:teuntje
  • 4
  • 3
LVL 13

Accepted Solution

SteH earned 50 total points
ID: 12141933
You can treet a long as char[4] and output it byte by byte. That way you should have full control only you need to find out if you cast these types whether bytes are swapped or not.

long lValue;
char pLong[4];
pLong = (char[4]) lValue;

union {
long lValue;
char cValue[4];
LVL 11

Assisted Solution

griessh earned 20 total points
ID: 12141974
Without knowing what you do it is hard to tell you how to make it right. Could you post the code taht creates those strings?

Author Comment

ID: 12141995
Could these lines have something to do with the byteorder (orry, I'm new to c / byteorder / programming)
#define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
#define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
Otherwise I should adapt these lines: (?):
            unsigned int i;
            for (i = 0; i < 16; i++)
                  printf("%02x", Mtext.buffer[i]);

Thanks again.
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

LVL 13

Expert Comment

ID: 12142229
It can well be that the two defines are messing things up. How are they used? The first returns an uint16 out of a char array and the other puts an uint16 into a char array. Depending on endianess this can change things.

Author Comment

ID: 12142950
There used in a function to convert to unicode:
static int _my_mbstowcs(int16* dst, uchar* src, int len)
      int      i;
      int16      val;

      for (i = 0; i < len; i++)
            val = *src;
            SSVAL(dst, 0, val);
            if (val == 0)
      return i;
But I don't see how I should adapt the code so that the ord is correct....
LVL 13

Expert Comment

ID: 12143027
This will, if at all change the two bytes in val. If the characters in the unicode string are readable you don't have a problem there. It is somewhere else. Mostlikely a similar cast but from int32 to char* (here is from (u)int16 to char*. Where does the original string come from?

Author Comment

ID: 12143205
There isn't a cast from int32...

char      text1[129];  
strcpy(text1, "eE&1");

The complete source is long (3 files, total over 500 lines...), but if you think it could help you (so, help me ), you can mail me at plinius at gmail dot com...

Author Comment

ID: 12143651
Solved. I found it. There where different outputfunctiond defined (and I used the wrong one, off course).
Thanks all for your help.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 72
C++ - Loading Managed Assembly From Memory in Unmanaged Process 25 470
What does std::atomic give me? 7 123
Grammars for C C++ and java 1 122
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question