Solved

Byte Conversion

Posted on 1998-06-16
10
218 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
  • 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 150 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now