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: -
short SwapShortWords( const short sWord )
-
{
-
union {
-
short iSwapped;
-
char acBytes[2];
-
}
-
WordIn, WordOut;
-
WordIn.iSwapped = sWord;
-
WordOut.acBytes[0] = WordIn.acBytes[1];
-
WordOut.acBytes[1] = WordIn.acBytes[0];
-
-
return WordOut.iSwapped;
-
}
-
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.
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.
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 -
short sElevation, mytest;
-
mytest = short((points[i].PropZ-pBenchmark[0].Z+10)/0.0025
-
sElevation = SwapShortWords(mytest);
-
iWriteReturn = write(gihFile, &sElevation,2);
-
-
What the heck is going on?
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.)
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;
}
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?
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??
BINGO!!!!!! I hadnt set file open to O_BINARY
Helalula ;)))))))))
This had been frustrating me for weeks.
Thanks for your help guys.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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,...
|
by: Rahul |
last post by:
Hi,
I have a little program as follows :
=================== STARTS HERE ================
#include <stdio.h>
void f (unsigned long);
int main()
{
|
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?
| |
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,
|
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
|
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
|
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
|
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.
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |