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 8244219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:46:37+00:00 2026-06-07T21:46:37+00:00

received help with. Basically, this code is written in Java but I need it

  • 0

received help with. Basically, this code is written in Java but I need it to be able to work in C as well. I have tested the code already and it works fine. It essentially takes a binary sting and places it into a byte array with each place holding 8 characters (1s and 0s). I do know a few variables that will never change and have listed them below:

  • Byte.SIZE is always 8
  • sLen is always 64
  • len is always 8

I have included the Java code and my attempt to make the conversion. However, my code consistently has run time errors and other syntax problems. I was wondering if anyone could find my issue:

Java code

static byte[] fromBinary(String s) {
    int sLen = s.length(), len = sLen / Byte.SIZE;
    if (sLen % Byte.SIZE != 0) {
        len++;
    }
    byte[] toReturn = new byte[len];
    for (int i = 0; i < sLen; i++) {
        if (s.charAt(i) == '1') {
            toReturn[i / Byte.SIZE] = (byte) (toReturn[i / Byte.SIZE] | (0x80 >>> (i % Byte.SIZE)));
        }
    }

    return toReturn;
}

C code

char fromBinary [] (String s) {
    int sLen =64;
    int len=8;
    int i=0;
    char toReturn [8];
    char str [64]=s;

    for (i = 0; i < sLen; i++) {
        if (str[i] == '1') {
            toReturn[i/8] = (char)(toReturn[i/8] | (0x80 >>> (i%8)));
        }
    }

    return toReturn;
}

OK sorry, so to begin I am relatively new to C. I have been programming in Java for a few years so I am not vey accustomed to the errors and syntax of C. I am using Dynamic C as the programming environment so error messages may be different.
I used the following main to run my program:

void main()  {
char bytes [8];
string s ="1010110011011110010010000010001101000101011001111010101111001101";
bytes=fromBinary(s);
 for (j=0;j<8;j++){
    printf("%d",toReturn[j]);
    printf(", ");
    }
}

I have been unable to run my program due to the following errors:

line    1 : ERROR UNTITLED2.C   : Only cofunctions can be indexed.
line    1 : ERROR UNTITLED2.C   : Old style function declaration missing parameter declaration list.
line    1 : ERROR UNTITLED2.C   : ',' is missing/expected.
line    6 : ERROR UNTITLED2.C   : Constant expression expected.
line   10 : ERROR UNTITLED2.C   : Invalid expression.
line   10 : ERROR UNTITLED2.C   : Missing character ';'.
line   10 : ERROR UNTITLED2.C   : Missing character ';'.
line   10 : ERROR UNTITLED2.C   : Missing character ')'.
line   10 : ERROR UNTITLED2.C   : Missing character ')'.
line   10 : ERROR UNTITLED2.C   : Invalid expression.
line   10 : WARNING UNTITLED2.C   : Conversion to incompatible pointer type
line   19 : WARNING UNTITLED2.C   : Type mismatch: incompatible types char[] and unsigned int used in expression.
10 errors reached; further errors suppressed.

Newest update

unsigned char *fromBinary(const char * const s) {
static unsigned char toReturn[8]={0};
size_t i,j;
const size_t len=8;
 for(i=0;i<len;i++)        {
    for(j=0;j<8;j++)
       toReturn[i]|=(s[i*8+j]=='1' ? 1<<(7-j) : 0);
    }
return toReturn;
 }

 void main()  {
 unsigned char bytes [8];
 string s ="1010110011011110010010000010001101000101011001111010101111001101";
 fromBinary(s)
 for (j=0;j<8;j++){
    printf("%d",toReturn[j]);
     printf(", ");
    }
}

gave me the following errors:

line    1 : ERROR UNTITLED3.C   : Keyword 'const' can only be used with globals and static locals.
line    4 : ERROR UNTITLED3.C   : Assignment to read-only variable not allowed.
line    4 : ERROR UNTITLED3.C   : Keyword 'const' can only be used with globals and static locals.
line    7 : WARNING UNTITLED3.C   : Conversion to incompatible pointer type
line    7 : WARNING UNTITLED3.C   : Conversion to incompatible pointer type
line    7 : WARNING UNTITLED3.C   : Conversion to incompatible pointer type
line   15 : WARNING UNTITLED3.C   : Type mismatch: incompatible types char[] and unsigned int used in expression.
line   15 : ERROR UNTITLED3.C   : Invalid expression - need lvalue.
line   15 : ERROR UNTITLED3.C   : s is out of scope/ not declared.
line   15 : ERROR UNTITLED3.C   : Missing character ';'.
line   15 : ERROR UNTITLED3.C   : string is out of scope/ not declared.
line   16 : WARNING UNTITLED3.C   : Conversion to incompatible pointer type
line   16 : WARNING UNTITLED3.C   : Wrong type for parameter 1.
line   16 : ERROR UNTITLED3.C   : s is out of scope/ not declared.
line   16 : ERROR UNTITLED3.C   : DynamicC does not support array assignment.
line   16 : ERROR UNTITLED3.C   : Invalid expression - need lvalue.
  • 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-06-07T21:46:40+00:00Added an answer on June 7, 2026 at 9:46 pm

    Just from the looks of it your C code is going to explode horribly (even if it were syntactically valid, which it is not). You’re returning toReturn which is allocated on the stack so it goes out of scope the moment the function returns. This is almost certainly going to cause segmentation violations and the like. Instead of trying to translate Java, why not just write it in C from scratch.

    unsigned char *fromBinary(const char * const s)
    {
       static unsigned char toReturn[8]={0};
       size_t i,j;
       const size_t len=8;
    
       for(i=0;i<len;i++)
           {
           toReturn[i]=0;
           for(j=0;j<8;j++)
              toReturn[i]|=(s[i*8+j]=='1' ? 1<<(7-j) : 0);
           }
       return toReturn;
    }
    

    Note the array returned is a) unsigned (to avoid sign extension) and b) allocated as static so it won’t go away outside the function.

    UPDATE: this is a complete test harness for the above function:

    #include <stdio.h>
    
    unsigned char *fromBinary(const char * const s);
    
    int main(void)
    {
       const char *const s="0010100100110101001101101101111010110010000001011101001001001001";
       unsigned char *b;
       b=fromBinary(s);
       printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7]);
       return 0;
    }
    
    unsigned char *fromBinary(const char * const s)
    {
    (as above)
    }
    

    This compiles without errors or warnings using gcc. It returns 29 35 36 DE B2 05 D2 49. In your code you are a) not declaring any header files b) not prototyping fromBinary() c) not declaring or assigning toReturn in your main() function. You should also never declare main() with void return type. If you get errors with the above code then either you are not using a real C compiler or it is broken. I suggest reading or re-reading a basic book on the C programming language. Kelley and Pohl’s ‘A Book on C’ is a good start.

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

Sidebar

Related Questions

I have a Person Class and based on some help I received in this
Evening all. I have the following code that I need looking into - basically
Ok I am still having some issues with this code below. I have received
I'm back with another Flex/Flash security question. I've already received some help from the
I have written a pretty simple code to get the first result for any
Help, I'm deperate..i can't continue my app because of this: I have setup one
Please help, I am stuck with this issue. Basically I am getting a AVATAR
I've been battling to get this right...basically I have the following HTML setup: <div
I need to process few lines of code over and over in RFT (java)
I have recently started to received this message. Unable to start debugging on the

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.