Solved

Byteorder messed up.

Posted on 2004-09-24
8
297 Views
Last Modified: 2010-04-01
Hi,
I made code which should output the following:
6959A3DAB8AB11179A6D65ED82F60598
but it outputs this:
DAA359691711ABB8ED656D9A9805F682
If you look wel,
you see that they are the same, but that the byteorder is just messed up
so, generally speaking:
ABCDEFGHIJKLMNOP
becomes:
DCBAHGFELKJIPONM
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.
0
Comment
Question by:teuntje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 13

Accepted Solution

by:
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;

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

Assisted Solution

by:griessh
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?
0
 
LVL 2

Author Comment

by:teuntje
ID: 12141995
Thanks.
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Expert Comment

by:SteH
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.
0
 
LVL 2

Author Comment

by:teuntje
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);
            dst++;
            src++;
            if (val == 0)
                  break;
            }
      return i;
}
_______________________
But I don't see how I should adapt the code so that the ord is correct....
0
 
LVL 13

Expert Comment

by:SteH
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?
0
 
LVL 2

Author Comment

by:teuntje
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...
0
 
LVL 2

Author Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

617 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