?
Solved

Byte Conversion

Posted on 1998-06-16
10
Medium Priority
?
254 Views
Last Modified: 2010-08-05
I have a floating point number (float) stored in a string of bytes (char).  For example, the 4th, 5th, 6th, and 7th position of a character string contains a float.  I need to take those four bytes and convert it into a float number.  I am able to do this with integers using shift operators, but can't get floats to work properly.  How can I do this?
0
Comment
Question by:ardolino
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1166032
How are they stored in the string?
As a memcpy?  With sprintf?  In IEEE format?
And what's the format of the float number on your machine?  
Does it differ from the format of the string?
0
 
LVL 4

Accepted Solution

by:
sganta earned 300 total points
ID: 1166033
Hai  !
I have the solution for your problem.

Let us assume that
test is a character string which contains floating number from 4th
to 7th

Include this function to convert into float

float get_float(char test[10000]) // or you can give char *test
{
   int j,len,i=4,intpart;
   char ch,ipart[4];
   float num,k,flt;  
   len = strlen(test);
   j=1;
   ipart[0] = '0';
   while (i<=7 && i<len && test[i] != '.')
   {
      ipart[j] = test[i];
      j++;
      i++;
   }

   // Now integer part is get selected
   ipart[j] = '\0';
   intpart = atoi(ipart); /* Or you can use your method to convert into
                             Integer Value */

   num = intpart; /* Or num = (float) intpart; */

   i++;
   k = 1;
   while (i<=7 && i<len)
   {
     flt = test[i]-48;  /* Or flt = (float)(test[i]-48); */
     k /=10;
     num +=k*flt;
     i++;
   }
     
   return num; // Returns the floating number
}            

I hope this is acceptable to you. This is tested and it works fine.

JESUS LOVES YOU. - sganta
0
 
LVL 84

Expert Comment

by:ozo
ID: 1166034
// one way it might be done:
#include <string.h>
float f=1234.5678;
char string[4+sizeof(float)];
memcpy(string+4,&f,sizeof(float));
memcpy(&f,string+4,sizeof(float));

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Expert Comment

by:ozo
ID: 1166035
// or, if the number was stored as sprintf(test+4,"%f",num)
#include <stdlib.h>
num=atof(test+4);
0
 
LVL 4

Expert Comment

by:sganta
ID: 1166036
Hai Ardolino !

There is small change in my answer. In C Array, it will store from index 0

float get_float(char test[10000]) // or you can give char *test
    {
       int j,len,i=3,intpart;  
       char ch,ipart[4];
       float num,k,flt;  
       len = strlen(test);
       j=1;
       ipart[0] = '0';
       while (i<7 && i<len && test[i] != '.')
       {
          ipart[j] = test[i];
          j++;
          i++;
       }

       // Now integer part is get selected
       ipart[j] = '\0';
       intpart = atoi(ipart); /* Or you can use your method to convert into
                                 Integer Value */

       num = intpart; /* Or num = (float) intpart; */

       i++;
       k = 1;
       while (i<7 && i<len)
       {
         flt = test[i]-48;  /* Or flt = (float)(test[i]-48); */
         k /=10;
         num +=k*flt;
         i++;
       }
         
       return num; // Returns the floating number
    }              
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166037
The easiest and most efficient way is to use pointers.  This will result in no code generation but, but will cause the compiler to treat the four bytes as a float rather than 4 characters.

char FloatStr[8];
float *FPtr =  (float *) &FloatStr[3];

FPtr now points to a float.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1166038
Assuming your machine has no alignment constraints on (float *)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166039
Ouch.  Good point.

Of course, this is not likely to be to too portable anyways.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1166040
True.  Although it depends on the storage format used, and whether you want the code to be portable but not the string, or the string to be portable but not the code.
(and it may also depend what was meant by "4th, 5th, 6th, and 7th positions"
Since C counts from 0, I'd tend to interpret it as "skipping the 0th, 1st, 2nd, and 3rd positions")

0
 

Author Comment

by:ardolino
ID: 1166041
The routine that you provided won't do what I want it to do.  The four bytes that are part of that string are the actual bytes, not a number written in a string.  For example, if it were an integer, i.e. 88, this refers to an X in the ASCII character table.  If I were to write those four bytes out to the screen, I would see an 'X' - if I then convert those four bytes to an integer, then I will see the number 88.

I am going to try and use the memcpy route - so far it seems to be working.  What are the potential problems with this?  You mentioned portability - what do you mean, exactly?  Anything else?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question