Solved

Byte Conversion

Posted on 1998-06-16
10
233 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

816 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

10 Experts available now in Live!

Get 1:1 Help Now