473,378 Members | 1,555 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,378 software developers and data experts.

Porting code to a different system, endianness, bytes swapping

3
Hi,
I'm trying to read data that generated in a big-endian system (SPARC) in the little-endian cpu (intel). The following is the code I used.

Please note that the following codes are fully working in SUN but not in the LINUX system I'm currently using.
===========================================
void Initialize(.... char *FreezeTableDSN)

{

int j, handle;

float h_clim[12][NO_OF_ROWS];
....
if( (handle=open( FreezeTableDSN, O_RDONLY ) ) == -1 ) {

perror("Open error for FreezeTable");

exit(1);

}

if( read( handle, h_clim, sizeof(h_clim) ) != sizeof(h_clim) ) {

perror("Read error for FreezeTable");

exit(1);

}
for(j=0; j<NO_OF_ROWS; j++ )
FreezeLevel[j] = h_clim[month-1][j];
...
============================================
(Freezelevel is declared as 'float' and month is declared as 'int')

The problem is that the data doesn't get read properly as a result of cpu system. Thus, I'm trying to swap the bytes for readability. However, I'm confuse as to how and what should I swap?

First, should I swap the whole stream of data before it gets translate into the the h_clim ? or should I translate cell-by-cell in the h_clim[month-1][j]?

Second, assuming that I need to translate cell by cell, I proceeded to define a macro
===============================
#define SWAP32(x) \

x = ((((x) & 0x00ff0000) >> 8) | \

(((x) & 0x0000ff00) << 8) | \

(((x) & 0xff000000) >> 24) | \

(((x) & 0x000000ff) << 24) )
===============================
However, the compiler says something like "Invalid operand to binary &" when I try to use the macro as for the h_clim or FreezeLevel.
E.G. SWAP32(FreezeLevel(j))

Any help would be greatly appreciate.

Thank you.
Jul 28 '07 #1
3 1911
ppanta
3
Sorry, forgot to note the detail of the machine and the compiler I'm using.

OS: Fedora 7
Compiler: native C compiler (I think it's GNU GCC)
Language: C
Jul 28 '07 #2
JosAH
11,448 Expert 8TB
Have a look at the htonl(), htons(), ntohl() and ntohs() macros.
The acronyms stand for: HostToNetwork and NetworkToHost; the 'l' and 's'
stand for Long and Short respectively. These macros convert an integer from
your system's endianness to network order and vice versa. The network order
is big endian per definition. (a la SPARC processors etc.)

kind regards,

Jos
Jul 29 '07 #3
ppanta
3
Thx, it solves the problem.
Jul 30 '07 #4

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

Similar topics

2
by: Chris Mantoulidis | last post by:
I have heard the way an int is stored, is different from the way a short is (for exampe eh... the same could apply for 16bit int and a long)... But how are they differently stored? Is the...
19
by: navin_2016 | last post by:
Hi, Can somone please describe the problems one would face when we try to port a software written in C to another platform some of them being endianness ,alignment,data types etc ? It would be...
26
by: Case | last post by:
#include <string.h> int i; /* 4-byte == 4-char */ char data = { 0x78, 0x56, 0x34, 0x12 }; int main() { memcpy(&i, data, 4); /*
16
by: Mohanasundaram | last post by:
Hi All, We are working on porting a product written in C and C++ from 32 bit to 64 bit. We need to maintain both 32 bit and 64 bit versions in the future. We took the 32 bit source code and...
15
by: T Koster | last post by:
Hi group, I'm having some difficulty figuring out the most portable way to read 24 bits from a file. This is related to a Base-64 encoding. The file is opened in binary mode, and I'm using...
18
by: friend.05 | last post by:
Code to check endianness of machine
6
by: Toby | last post by:
As part of a program I'm writing, I need to save to disk big amounts of data (hundreds of MB, in 8kB chunks) swapping every couple of bytes. I obviously cannot do it in a Python loop. Is there...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
6
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.