GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jul-2005, 13:47
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road

Changing big/little endian


Hi,

My program is supposed to work in both machines with little endian and big endian. My code for chaning the endian of short integers seems to be fine, but I am getting weired results for chaning floats. Here is the corresponding part of the code:

CPP / C++ / C Code:

#include <iostream>
                         
using namespace std;

int main()
{
	float val;
	cout << "Please enter a float: ";   
	cin >> val;
	char temp;
	union int_to_char
	{
	  float floatVal;
	  char byte[4];
	}
	conv;
	conv.floatVal = val;
	temp = conv.byte[0];
	conv.byte[0] = conv.byte[3];
	conv.byte[3] = temp;
	temp = conv.byte[1];
	conv.byte[1] = conv.byte[2];
	conv.byte[2] = temp;
  val = conv.floatVal;

	cout << "val: " << val << endl;
	
return 0;
}

/*output :
Please enter a float: 369
val: 1.1821e-38

or

Please enter a float: 155
val: 9.77966e-42
*/


Could anyone please let me know what the problem is?

Thank you,

Nina
  #2  
Old 22-Jul-2005, 17:30
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by Dream86
Could anyone please let me know what the problem is?
What makes you think there is a problem?
  #3  
Old 22-Jul-2005, 18:07
Dream86 Dream86 is offline
Junior Member
 
Join Date: Jun 2004
Posts: 54
Dream86 is on a distinguished road
Quote:
Originally Posted by Dave Sinkula
What makes you think there is a problem?

Should the output be such a a small numbers (in order of -40 to -38 ) , and sometimes i get negative numbers back.

Nina
  #4  
Old 24-Jul-2005, 21:13
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by Dream86
Should the output be such a a small numbers (in order of -40 to -38 ) , and sometimes i get negative numbers back.
If you're concerned about the bits of the object representation, look there. Values, once bits are twiddled, may or may not appear to make sense.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
changing colour of individual items of control created through CListCtrl Sadia MS Visual C++ / MFC Forum 0 01-Jul-2005 09:09
Changing dialog template oz_dkj C++ Forum 0 23-May-2005 08:47
Changing the Properties of a Control Programmatically ddod MS Visual C++ / MFC Forum 10 11-Aug-2004 10:26
Problems with changing display resolution vsseym Computer Software Forum - Windows 2 27-Jul-2004 11:01
changing default webpage info mkyb14 Apache Web Server Forum 2 12-Nov-2003 04:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.