• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Byte Conversion

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
ardolino
Asked:
ardolino
  • 5
  • 2
  • 2
  • +1
1 Solution
 
ozoCommented:
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
 
sgantaCommented:
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
 
ozoCommented:
// 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ozoCommented:
// or, if the number was stored as sprintf(test+4,"%f",num)
#include <stdlib.h>
num=atof(test+4);
0
 
sgantaCommented:
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
 
nietodCommented:
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
 
ozoCommented:
Assuming your machine has no alignment constraints on (float *)
0
 
nietodCommented:
Ouch.  Good point.

Of course, this is not likely to be to too portable anyways.
0
 
ozoCommented:
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
 
ardolinoAuthor Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now