Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7164655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:06:38+00:00 2026-05-28T14:06:38+00:00

So, I am having trouble with some code. I want this function to take

  • 0

So, I am having trouble with some code.

I want this function to take in a byte array(testing with single byte for now), convert the byte into binary and then append it to a “1.” to use in a calculation.

ex:

ouput: 01110000 —-> 1.01110000

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

double calcByteValue(uint8_t data[], int size);

int main() {
   uint8_t test[10];
   test[0] = 0x0e;

   double d = calcByteValue(test, 8);
   return 0;
}

double calcByteValue(uint8_t data[], int size) {
    int i;
    uint8_t bits[21];
    char binary[100];
    char str[100] = "1.";

    for (i = 0;i < size;i++) {                    
            bits[i] = (data[0] >> i) & 1;
            if (bits[i] == 0) {
                binary[i] = '0';
                printf("0(%d)\n", i);
            } else {
                binary[i] = '1';
                printf("1(%d)\n", i);
            }
    }

    strcat(str, binary);
    float d = atof(str);
    printf("%f\n", d);
    return 0;
    //return pow(-1, bits[0]) * pow(2, (128-127)) * atof(str));
}

Here is my output, for some reason it is going through the whole loop just fine, but only printing 6 of the original bits, knocking off the last couple ones. What am I doing wrong???

0(0)
1(1)
1(2)
1(3)
0(4)
0(5)
0(6)
0(7)
1.011100
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:06:40+00:00Added an answer on May 28, 2026 at 2:06 pm

    First:

    strcat(str, binary);
    

    You never null terminated the binary array. You have to to use it as a string.

     char binary[100];
    

    binary is defined at block scope, and block scope automatic objects that are not explicitly initialized have an indeterminate value.

    Here is how to null terminate the array:

    binary[size] = '\0';
    

    Second:

    you are inserting the bits values in the reverse order. Put (data[0] >> 7) & 1 for binary[0], (data[0] >> 6) & 1 for binary[1] and so on.

    Also printf with %f conversion specification prints 6 digits after the decimal point. If you want more digits (e.g., 16), you can specify the precision like this: printf("%.16f\n", d);

    You are also using the type float for object d, if you find you don’t have enough precision with float, you can use type double.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble with this code . The problem is when i
im having some trouble with the following code: Ext.define('...controller...', { extend: 'Ext.app.Controller', init: function()
This should be simple but I'm having trouble changing some C code that uses
I'm writing some code in python and I'm having trouble when trying to retrieve
I am porting some C# code over to Java. I am having trouble with
I'm having some trouble understanding a bit of code. I've got 2 classes Company
I am having some trouble trying to checkout the BitSharp source code using SVN.
I'm having some trouble with the jQuery hover method. Here's the relevant JavaScript code:
I'm having some trouble with finding the visibility param for JQuery. Basically... the code
I having some trouble figuring this problem out where I have one button which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.