Double data type C Red Hat platform problem


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Double data type C Red Hat platform problem
# 1  
Old 10-05-2015
Error Double data type C Red Hat platform problem

I'm converting a binary file to ASCII using c code.
The folllowing block of code prints correct double value 00000.000000000 on HPUNIX platform.

Code:
 
longi double;
/* C79 - Price Per Minute */  
  memcpy(&longi,&rbuff[503],8);
  fprintf(wfp,"%015.9f ",longi);

prints :
Code:
00000.000000000

however , the same block when executed on Red hat (
gcc version 4.1.2 20080704 (Red Hat 4.1.2-50)
) is not portable with the platform.It prints random digits with length > 180 like 123234342343433423423423423423423423434.3424342342343434234


is there a way make the code working on red hat ?
# 2  
Old 10-05-2015
I'm guessing, but I think one problem is Endianness. The byte order of the 8 bytes copies into the double variable is inverted. Also note you have more than one thing going on here. It looks like you may have compiled K&R code.

1. you are messing with the order of the bytes
2. memcpy may not be filling the destination double variable with everything it need in the right places.

Either one of these is called 'undefined behavior'. Undefined behavior means the compiled has no idea what to do with the garbage in the variable. You have to have incurred some kind of exception.

Also note: HPUX uses an ancient version (K&R) C compiler by default which may be why it 'works' there - probably some odd library rather than FP opcodes. Red Hat uses x86 architecture - it has FP operations on FP registers, not a library.

Last edited by jim mcnamara; 10-05-2015 at 10:51 AM..
# 3  
Old 10-05-2015
What exactly are the contents of rbuff, anyway?
# 4  
Old 10-05-2015
The code:
Code:
longi double;
/* C79 - Price Per Minute */  
  memcpy(&longi,&rbuff[503],8);
  fprintf(afp,"%015.9f ",long);

isn't valid in any C compiler I've ever seen. The 1st line is declaring a variable named double of type longi.

Are we correct in assuming that the 1st was actually:
Code:
double longi;

What hardware were you using on the system running HP/UX? In addition to the endianness issue mentioned by Jim McNamara, there can also be differences in byte order within an object of type double when you move from one CPU type to another.
# 5  
Old 10-06-2015
Apologies "double longi;" is not part of the code , i just wanted to inform that longi is of double data type.
i don't have any hardware info for the HP system.

the field i need to extract from binary file is of 8 bytes . it starts from 503 to 511 bytes in the binary file.So i read 8 bytes into longi and then print it .

Code:
unsigned char rbuff[1012];
double longi

 /* C79 - Price Per Minute */  
  memcpy(&longi,&rbuff[503],8);
  fprintf(wfp,"%015.9f ",longi);

# 6  
Old 10-06-2015
It likely needs to be byte-swapped.

Code:
#include <stdint.h>

void swap64(void *mem) {
        uint64_t x=*((uint64_t *)mem)
        x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
        x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
        x = (x & 0x00FF00FF00FF00FF) << 8  | (x & 0xFF00FF00FF00FF00) >> 8;
        *((uint64_t *)mem)=x;
}

...

memcpy(&longi,&rbuff[503],8);
swap64(&longi); // Only do this on systems of differing endian from file

Code adapted from here.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 10-09-2015
It worked.

Thank you all.
This User Gave Thanks to krk For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Fedora

Which is the better platform to learn UNIX/Linux (Kali Linux Vs. Red Hat or other)?

I just started a new semester and I started my UNIX class yesterday. I've already decided to use python along with my learning process but what I really want to use with it is Kali as my UNIX/Linux platform to learn off of since I already wanted to learn Cyber Sec. anyways. I just wanted to know if... (12 Replies)
Discussion started by: ApacheOmega
12 Replies

2. Infrastructure Monitoring

Problem Installing Cacti on Red Hat

Can someone please point me in the direction of where I can find out how to install cacti? there doesn't seem to be straightforward steps for it. after downloading the tar.gz file from cacti.net, there's basically no instructions of what to do from that point on. any help will be appreciated. ... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Red Hat

Clustering Red Hat Enterprise linux Advanced Platform 5.2 (x86-64 bit)

Hello Professionals, We have high-end HP rack servers. We need to provide application fail-over for business continuity. We have done benchmarking of the application on RHEL 5.2 on HP servers to estimate the hardware requirement so as to meet next 3-5 years business growth. Presently we... (1 Reply)
Discussion started by: kgayyar
1 Replies

4. UNIX for Dummies Questions & Answers

Problem Red Hat 9 Installation

I have a problem with a red hat 9 install on a compaq laptop. I installed the OS and everything tests ok but It boots to a blank screen instead of the GUI login I wanted. What can I do to correct this instead of installing all over. I tried the rescue option several times and tried to run... (2 Replies)
Discussion started by: vedder191
2 Replies

5. Linux

Installation Problem on Red hat linux 9

Hi, I am trying to install our new acquired Red Hat linux 9 on Compaq Proliant ML370. However, l has already configured the raid 0+1. The linux installation starts from the CD, its allows for the selection of Language, keyboard but l am stocked at choosing the installation package media. I... (4 Replies)
Discussion started by: kayode
4 Replies

6. Linux

Red Hat RPM problem

I have my system setup different then the conventional setup: # df -h Filesystem Size Used Avail Use% Mounted on /dev/hdc2 2.0G 977M 936M 52% / /dev/hdc1 97M 9.0M 83M 10% /boot none 251M 0 251M 0% /dev/shm So when I try... (2 Replies)
Discussion started by: jasonr
2 Replies

7. UNIX Desktop Questions & Answers

Red Hat loading problem

when i try to load Redhat 7.2 the screen gets all mished together as if it were a meltdown the book says its a prob with X but it dosnt really say what to do WINMe works fine i have a radeon 64 MB card 1 gig processor and 2 HDs on a Dell 1 GHZ how do i fix this ? (1 Reply)
Discussion started by: ASpin
1 Replies

8. UNIX for Dummies Questions & Answers

Linux Red Hat 7.2 Problem

how come red hat doesn't have this programs which was already supposed to be on it?? take for instance, I was reading this Linux book that says for me to do some work in glint X11 client for packagement. it says glint is accessible from the command line or control panel. the problem is, I... (4 Replies)
Discussion started by: TRUEST
4 Replies

9. UNIX for Dummies Questions & Answers

Please Help Me With This Linux Red Hat 7.2 Modem Problem

I cant get my modem to work. Linux just isn't recognizing it. every time I typed wvdial, i get a message saying my modem is not responding I tried looking for drivers at the website Neo directed me to but I just couldn't find the right one from the information I was given about my modem... (2 Replies)
Discussion started by: TRUEST
2 Replies

10. UNIX for Dummies Questions & Answers

Problem Downloading Red Hat

I've been having problems downloading Red Hat 7.2 from their FTP site. It downloads rather slowly(between 2-3k/sec, I'm on broadband) and after about 10 minutes stops downloading altogether. Am I doing something wrong? (2 Replies)
Discussion started by: Tradewynd
2 Replies
Login or Register to Ask a Question