Solved

Byte Conversion

Posted on 1998-06-16
10
242 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
Industry Leaders: 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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 145
thread-safe code in c++ 2 122
Problem with SqlConnection 4 184
Where are the c++ header files to use in Eclipse? 4 53
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

730 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