LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2005, 12:27 AM   #1
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
ASCII to float


Hi,
if I have a string say
C203145B (32-bit)
I want to convert it to a float
My attempt was

long temp;
float ret;
long *cast;

cast = (long *) &ret;
unsigned int idx = DataIndex * 8;

temp = (long) AsciiToHex (m_pData[idx]) << 28 |
(long) AsciiToHex (m_pData[idx+1]) << 24 |
etc

*cast = temp;
return ret;

it is working well in Windows but return a small fraction number in Linux.... Is it some big/small endian problems here?
Thanks
Jack
 
Old 12-14-2005, 11:23 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

double hex2double(const char *hex)
{
    char *tmp;
    
    if ((tmp = malloc(strlen(hex) + 3)) == NULL) {
        perror("hex2double()");
        exit(1);
    }
    if (hex[0] != '0' || hex[1] != 'x' || hex[1] != 'X') {
        sprintf(tmp, "0x%s", hex);
    } else {
        strcpy(tmp, hex);
    }
    return atof(tmp);
}

int main()
{
    double floatnum;

    floatnum = hex2double("C203145B");
    printf("Resulting double value = %f\n", floatnum);
    return 0;
}
Output:
double value = 3254981723.000000
 
Old 12-15-2005, 01:51 AM   #3
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
How about this?

union hex2str{
float f;
char ch[4];
} h2s;

strcpy(ret,h2s.ch);
printf("%f",h2s.f);

You may have to flip the ch[0] with ch[3], ch[1] with ch[2] depending on your system's byte order.
 
Old 12-15-2005, 02:52 AM   #4
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
Thanks for waking me up.
Jack
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[C++] char * to float Ossar Programming 2 08-03-2005 10:28 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
how big is a float in C? SciYro Programming 11 04-04-2005 09:24 AM
How to use a float in a script. philipina Programming 4 03-18-2004 08:06 AM
C newbie float ? bluesky Programming 7 09-03-2003 12:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration