Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > converting a character array to a float (newbie)

Reply
Thread Tools

converting a character array to a float (newbie)

 
 
Matthew R. Dempsky
Guest
Posts: n/a
 
      09-07-2006
On 2006-09-07, Jim Bancroft <(E-Mail Removed)> wrote:
> char buffer[128];
> int n;
>
> bzero(buffer,12
> n=read(myFD,buffer,127);
>
> printf("You read in a value of %f\n",buffer); /*strange output here*/


buffer is a char pointer here, not a floating point value. Try:

printf ("You read in a value of %f\n", *(float *)buffer);

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Jim Bancroft
Guest
Posts: n/a
 
      09-07-2006
Hi everyone,

Trying to figure something out...I have a character buffer that I fill up
using read(). I know that what's coming back is in fact a float, but I
can't seem to convert and print it out correctly. Here's a snippet:

char buffer[128];
int n;

bzero(buffer,12
n=read(myFD,buffer,127);

printf("You read in a value of %f\n",buffer); /*strange output here*/


....pretty straightforward I guess. I should mention the buffer is 128 bytes
long because sometimes I'm reading in text. But in the circumstance above I
know for a fact it'll be a float that's coming in.

I've tried a few things but no luck so far. Could someone point me in the
right direction? Thanks very much.


 
Reply With Quote
 
 
 
 
jmcgill
Guest
Posts: n/a
 
      09-07-2006
Jim Bancroft wrote:
> char buffer[128];
> int n;
>
> bzero(buffer,12
> n=read(myFD,buffer,127);
>
> printf("You read in a value of %f\n",buffer); /*strange output here*/
>
> I've tried a few things but no luck so far. Could someone point me in the
> right direction? Thanks very much.


You are getting 32 bits of IEEE-754 float, and the byteorder is correct
for your platform?


Have you tested that strategy with known values in your buffer, in that
printf?

I think a better approach to this problem might be with a union,
something like this (from my rusty memory):

union {
float f;
unsigned char buf[MAX_WIDTH];
} u;

Then you could read the value into u.buf and reference it as u.f
But byteorder will still be a problem.

Look at the source on this page:
http://www.answers.com/topic/ieee-fl...point-standard

They are explicitly putting each character in place, masking out the
sign, exponent, and mantissa, and returning the result as a float.
 
Reply With Quote
 
Jim Bancroft
Guest
Posts: n/a
 
      09-07-2006
Thanks for your help. Unfortunately, when I do that I always get a result
of 0.000000, no matter what the actual value being read back?

I don't know if this makes any difference but the "myFD" descriptor
referenced below is actually a socket. It's being filled by a java socket
server using java's PrintWriter class. In this instance the PrintWriter is
constructed from a Socket's getOutputStream() method. I know this isn't a
java newsgroup but I thought I'd toss that in, for purposes of full
disclosure.



"Matthew R. Dempsky" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2006-09-07, Jim Bancroft <(E-Mail Removed)> wrote:
>> char buffer[128];
>> int n;
>>
>> bzero(buffer,12
>> n=read(myFD,buffer,127);
>>
>> printf("You read in a value of %f\n",buffer); /*strange output here*/

>
> buffer is a char pointer here, not a floating point value. Try:
>
> printf ("You read in a value of %f\n", *(float *)buffer);
>
> --
> Posted via a free Usenet account from http://www.teranews.com
>



 
Reply With Quote
 
Ancient_Hacker
Guest
Posts: n/a
 
      09-07-2006

Jim Bancroft wrote:
> Hi everyone,
>
> Trying to figure something out...I have a character buffer that I fill up
> using read(). I know that what's coming back is in fact a float, but I
> can't seem to convert and print it out correctly.


Exactly what is being sent by the other end? Are they characters:
"3.14159",
or binary float values?

If binary, how will you ensure the float format on the sending end is
the same on the receiveing end?

If they're characters, you must do a atof() or sscanf() to convert the
characters to a float.

If you're receiving binary, you have to be sure the sending and
receiving formats are identical. Not just both IEEE standard, but sent
in the same byte ordering.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-07-2006
"Jim Bancroft" <(E-Mail Removed)> writes:
> Trying to figure something out...I have a character buffer that I fill up
> using read(). I know that what's coming back is in fact a float, but I
> can't seem to convert and print it out correctly. Here's a snippet:
>
> char buffer[128];
> int n;
>
> bzero(buffer,12
> n=read(myFD,buffer,127);
>
> printf("You read in a value of %f\n",buffer); /*strange output here*/
>
>
> ...pretty straightforward I guess. I should mention the buffer is 128 bytes
> long because sometimes I'm reading in text. But in the circumstance above I
> know for a fact it'll be a float that's coming in.
>
> I've tried a few things but no luck so far. Could someone point me in the
> right direction? Thanks very much.


bzero() and read() are not standard C functions. For maximal
portability (and if you want advice in this newsgroup), use memset()
and fread() instead.

It probably makes more sense for buffer to be an array of unsigned
char rather than plain (possibly signed) char.

Try displaying the elements of buffer as integers:

/* untested code */
for (i = 0; i < sizeof(float); i ++) {
printf("buffer[%d] = %d\n", i, buffer[i]);
}

If the results aren't what you expect, then the problem is probably
somewhere outside the scope of standard C.

You mention elsewhere that you're reading from a socket. Look at
whatever is on the other end of that socket. (Sockets are off-topic
here.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
jmcgill
Guest
Posts: n/a
 
      09-07-2006
Jim Bancroft wrote:
> Thanks for your help. Unfortunately, when I do that I always get a result
> of 0.000000, no matter what the actual value being read back?


I would do this; Put a known float value on this I/O. Evaluate it by
hand to see that it is indeed what you are expecting.

You are using java.io.PrintWriter to create the input. Are you
controlling the character encoding? That is, are you sure it's plain
bytes, not UTF-16?

 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      09-07-2006
Jim Bancroft schrieb:
> Thanks for your help.


Please don't top-post. See my signature.

> Unfortunately, when I do that I always get a result
> of 0.000000, no matter what the actual value being read back?


Did you check the return value of read()? If read() didn't read something
and returns an error, the buffer would still be zero-filled.

> I don't know if this makes any difference but the "myFD" descriptor
> referenced below is actually a socket. It's being filled by a java socket
> server using java's PrintWriter class.


http://java.sun.com/j2se/1.4.2/docs/...intWriter.html

"Print formatted representations of objects to a text-output stream. This
class implements all of the print methods found in PrintStream. It does not
contain methods for writing raw bytes, for which a program should use
unencoded byte streams."

It seems that this class is not designed to write binary output. But since
that is offtopic here, I won't go further into this.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
 
Reply With Quote
 
jmcgill
Guest
Posts: n/a
 
      09-07-2006
Ancient_Hacker wrote:

> If binary, how will you ensure the float format on the sending end is
> the same on the receiveing end?


He mentioned that it's java.io.PrintWriter, which has a print(float)
method which stringifies the number and puts it on the output stream as
text, in whatever character encoding is applicable to the locale.

Even if he's lucky, and that character encoding is UTF-8, he is
basically trying to cast a string of characters as a C float, which
won't work.

> If they're characters, you must do a atof() or sscanf() to convert the
> characters to a float.


Based on the info so far, I'm sure that's the answer. However, it is
not necessarily in a single-byte character set!


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-08-2006
On 07 Sep 2006 20:34:55 GMT, "Matthew R. Dempsky" <(E-Mail Removed)>
wrote:

>On 2006-09-07, Jim Bancroft <(E-Mail Removed)> wrote:
>> char buffer[128];
>> int n;
>>
>> bzero(buffer,12
>> n=read(myFD,buffer,127);
>>
>> printf("You read in a value of %f\n",buffer); /*strange output here*/

>
>buffer is a char pointer here, not a floating point value. Try:
>
> printf ("You read in a value of %f\n", *(float *)buffer);


And then pray that buffer is properly aligned for a float.


Remove del for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting an array of string to array of float joy99 Python 7 03-26-2011 12:07 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
converting 'float (*)[4]' to 'const float**' mathieu C++ 9 09-15-2007 09:10 AM
converting a signed float into unsigned float Dipesh Batheja Ruby 1 11-16-2006 07:29 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments