-4

I have a binary message on a string. The string contains values such as "10001000" and it's being used as a buffer to store binary representations of different data types. I will parse the data and then convert each sequence. My struggle is to convert an specific string of 1s and 0s into: integer, float or double. A possible solution for the int is strtol, but it doesn't solve all my problem...

I've read throw many discussions of people trying to create a string containing the binary representation of a number. But my issue is exactly the opposite: roll back the number once I have a string with it's binary representation.

Any thoughts or advices will be most welcome.

ppf
  • 1
  • 1
    so, you're telling you din't get the answer (ok, ok, hint, atleast) by searching SO? – Sourav Ghosh May 13 '15 at 19:35
  • 1
    I *honestly* can't believe googling for something relevant did **never** lead you to [`strtol()`](http://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html)… – The Paramagnetic Croissant May 13 '15 at 19:37
  • 2
    Typed "convert binary to decimal in c" in the search box. Received tons of already asked questions with the same matter – mcleod_ideafix May 13 '15 at 19:37
  • It did, strtol works fine. The thing is can I do the same for other types or just write down my own converter? For example, strtod only work if i have the number as string (not bits of the number as string). Well, I still think it's a valid question. – ppf May 13 '15 at 19:47
  • 1
    ouh, is google down again ? – Jabberwocky May 13 '15 at 20:31
  • He's asking how to convert a binary number in string format into the actual number. – Daniel Rudy May 14 '15 at 05:23
  • Exactly what Daniel said. If it's an integer the job is quite easy with strtol (like many have "kindly" remarked). Nevertheless, for floating point this specific function won't work. – ppf May 14 '15 at 14:36

2 Answers2

0

This is quite simple really...

unsigned int number;
char str[33];
int i;
int s;

/* Read some 32-bit binary number in character representation in str. */
Do some stuff.

/* Convert from string to binary. */
number = 0;
s = strlen(str);
for (i = 0; i < s; i++)
  {
    if (str[i] == 0x00) break;
    if (str[i] == '0')
      {
        number |= 1;
        number << 1;
      }
    if (str[i] == '1')
      {
        number &= 0xFFFFFFFE;
        number << 1;
      }
  }

Generally, you can find this on Google or in your textbook. So you got this for free, this time.

EDIT:

I noticed that you also mentioned float or double as well. Those are complicated data types that are defined by the IEEE-754 standard. If the binary string is already in floating point representation, then you can just use a union to get from an unsigned int to one of the float types. Code below:

union float_conversion_t
  {
    unsigned int i[2];
    float f;
    double d;
  };

Here you datafill one or both integers and then read the float or double value. You may need to adjust the size of the array depending on the word size of your machine (32-bit, 64-bit, or something else).

Daniel Rudy
  • 1,411
  • 12
  • 23
0

Thank you to Daniel and all other valid comments.

The main question was if float/double conversions were possible with any preexisting function. My understanding after all this (correct me plz if I'm wrong) is no.

So one must go trough the math and conversion for floating points (lots of documentation on IEEE 754 online...) and use strtol for integers. Fair enough.

It may be of help to others:

  • Here's a conversion of binary IEEE 754 as string to double
  • But if you look for converting something like "1001.1101" (binary point representation) to float, this guy did a nice job
Community
  • 1
  • 1
ppf
  • 1