返回

使用二进制字节转换代码将 C 转换为 Java

发布时间:2022-07-14 23:49:54 213
# 移动端

基本上,这段代码是用 Java 编写的,但我也需要它能够在 C 中工作。我已经测试过代码并且工作正常。它本质上需要一个二进制字符串并将其放入一个字节数组中,每个位置包含 8 个字符(1 和 0)。我确实知道一些永远不会改变的变量,并在下面列出了它们:

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

我已经包含了Java代码和我进行转换的尝试。然而,我的代码始终存在运行时错误和其他语法问题。我想知道是否有人能找到我的问题:

Java代码

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代码

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;
}

好的,很抱歉,首先我对C比较陌生。我已经用Java编程几年了,所以我不太习惯C的错误和语法。我使用动态C作为编程环境,因此错误消息可能不同。我使用以下main来运行我的程序:

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

由于以下错误,我无法运行程序:

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.

最新更新

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(", ");
    }
}

给了我以下错误:

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.
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(2)
按点赞数排序
用户头像