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.SIZEis always 8sLenis always 64lenis 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.
>>>operator?