473,788 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Signed little-endian-big-endian problems

5 New Member
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 3667
donbock
2,426 Recognized Expert Top Contributor
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
graemejcox
5 New Member
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 Recognized Expert Top Contributor
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
graemejcox
5 New Member
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.iSwappe d;
}
Nov 6 '09 #5
Banfa
9,065 Recognized Expert Moderator Expert
Your number, -756, 2500 etc? How are you getting them?

Are you just printing them before and after conversion?
Nov 6 '09 #6
graemejcox
5 New Member
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
graemejcox
5 New Member
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
1982
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 when the session times out or they just close the browser. Thx. Steve.
12
8512
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. Can someone tell me how to sign js to work with with ie? Can i put my js file into jar and use jarsigner for that? And by the way, would it give me disk access permissions? I need that for the following: our client gets a cd from us with some mp3...
16
2822
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 one hand, the warning *could* be useful. Most of the time I get it in cases where I know the comparison is safe, but it's not hard to imagine that this won't always be the case. This makes disabling it undesirable. Casting is a workable solution,...
2
2138
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
4637
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 think). I don't know of any character sets that use negatives, e.g. 65 is 'A' and -65 is 'a'?!? I'm sure I'm missing something, any ideas?
11
2683
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 CountOccurrences(unsigned char const val, unsigned char const p,
14
14309
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: 0xffffff81 Octal: O37777777601 Binary: 1098 7654 3210 9876 5432 1098 7654 3210
10
3311
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 signed/unsigned modifier to class declarations to next revision of C++ programming language
7
5049
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 involved. The problem is that comparisons between signed and unsigned values are machine- dependent, because they depend on the sizes of the various integer types. For example, suppose that int is 16 bits
4
4329
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 unsigned char buffer instead of a signed char buffer? Also if I want to write this to file. Casting the buffer to "char *" & then using fprintf seems to work, but this doesn't seem right to me.
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.