473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Signed little-endian-big-endian problems

Hi.
I am having some problem converting some signed shorts from little endian (intel) to big (motorola chip).

Im using this code but it appear to produce errors when I convert some negative shorts:

Expand|Select|Wrap|Line Numbers
  1. short SwapShortWords( const short sWord )
  2. {
  3.           union {
  4.                    short iSwapped;
  5.                    char acBytes[2];
  6.                 }
  7.            WordIn, WordOut;
  8.            WordIn.iSwapped = sWord;
  9.            WordOut.acBytes[0] = WordIn.acBytes[1];
  10.            WordOut.acBytes[1] = WordIn.acBytes[0];
  11.  
  12.            return WordOut.iSwapped;
  13. }
  14.  
Eg when I convert values near 0, -256, -512, -756 I get converted values around +2500, +2800, etc. Note these values are approximate ie I have an idea of where the problems are occuring but not the exact values.

I think it has something to do with the sign bit. Can anyone suggest a modification to my code to solve this problem?

thanks in advance.
Nov 5 '09 #1
7 3642
donbock
2,426 Expert 2GB
You should never use plain char for numeric values; you should use either signed char or unsigned char. In this case unsigned is probably better.

Using a union to obtain the native endianness makes me wince, but it ought to work in this case so I won't make any waves.
Nov 5 '09 #2
Don
Thanks for the response. I tried 'unsigned short' with no improvement.

I have been bashing my head against the wall trying to solve this and still havent. Further info I found was:

I tired with positive values between about 3000 and 4000 to see if the negative was the root of the problem but it appears it is not.

Most values swap OK but these input values below produce the returns values.

Input Returns
3082 2566 2573 2567
3338 2812 2561 2573 2565
3593 2802 2803
3594 2803

Note the return results dont seem to be fully consistant bit similar eg 3082 returns three values 2566, 2573, 2567.

Also note the return values are actually swapped, then written to file, file closed and reopened and then I read them back. The writing to file might be something to do with the problem too. To write I use

Expand|Select|Wrap|Line Numbers
  1. short sElevation, mytest;
  2. mytest = short((points[i].PropZ-pBenchmark[0].Z+10)/0.0025    
  3. sElevation = SwapShortWords(mytest);
  4. iWriteReturn = write(gihFile, &sElevation,2); 
  5.  
  6.  
What the heck is going on?
Nov 6 '09 #3
donbock
2,426 Expert 2GB
Let me repeat myself more succinctly.
Use unsigned char in the definition of the union.

(It doesn't matter if you use short or unsigned short in the union.)
Nov 6 '09 #4
Don
Soory. I meant to say I tried unsigned char:

short SwapShortWords( const short sWord )
{
union {
short iSwapped;
unsigned char acBytes[2];
}
WordIn, WordOut;
WordIn.iSwapped = sWord;
WordOut.acBytes[0] = WordIn.acBytes[1];
WordOut.acBytes[1] = WordIn.acBytes[0];

return WordOut.iSwapped;
}
Nov 6 '09 #5
Banfa
9,065 Expert Mod 8TB
Your number, -756, 2500 etc? How are you getting them?

Are you just printing them before and after conversion?
Nov 6 '09 #6
Banfa
Hi. Thanks for having a look.

To get the return values (eg 2500) I actually swapped the input (eg 756) , then write to binary file, 1000s of other values are written to file in the same way, file is closed and reopened and then I read them back.

To write them I use:
short sElevation, mytest;
mytest = short((points[i].PropZ-pBenchmark[0].Z+10)/0.0025
sElevation = SwapShortWords(mytest);
iWriteReturn = write(gihFile, &sElevation,2);


Thinking about it more, the writing to file might be something to do with the problem too because if I swap the values using:

mytest = short(3338);
sElevation = SwapShortWords(mytest);
mytest = SwapShortWords(sElevation);

It seems to work fine but if I go through the full procedure then it doesnt.

The other interesting I just found was:
SwapShortWords(3338) is equal to 2573 (0x0d0a to 0x0a0d)
and
SwapShortWords(3594 ) is equal to 2574

Wow. Is that a coincidence: 0x0d0a is equal to CRLF in DOS/Windows?????

OK CRLF is my lead now. Im looking into this. Any ideas??
Nov 6 '09 #7
BINGO!!!!!! I hadnt set file open to O_BINARY
Helalula ;)))))))))
This had been frustrating me for weeks.

Thanks for your help guys.
Nov 6 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: szar | last post by:
Anyone know a good way to check if someone is signed in, has a session going, etc.? I could have a signed in field that updates from 'yes' to 'no' when they sign out, but that wouldn't work well...
12
by: Andrey | last post by:
Hi, I just wonder if anybody can help me with this: I need to give my javascript code some extended permissions for disk access on client's machine, and as i understand i need to sign this js....
16
by: Kevin Goodsell | last post by:
What do you think is the best way to handle a compiler warning about comparing an unsigned value to a signed value? Cast to silence it? Disable that warning altogether? Or just live with it? On...
2
by: Rahul | last post by:
Hi, I have a little program as follows : =================== STARTS HERE ================ #include <stdio.h> void f (unsigned long); int main() {
64
by: ng5000 | last post by:
Hi, What's the point of a signed char? As I see it a char represents a character (not an integer, use an int type e.g. short int if you want an 8 bit number, or one of the new types, uint8 I...
11
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int...
14
by: me2 | last post by:
I am writing a little base conversion utility called base.c. This is what base does. $ base -127 Signed decimal: -127 Unsigned decimal: 4294967169 Hexidecimal: ...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
4
by: Sam | last post by:
Hi, I am using some functions return a base64 encoded string. The functions write it into an unsigned char buffer. I am a little confused as to why a base64 encoded string would be using an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.