简体   繁体   English

java转换为C / C ++(硬件c / c ++)转换

[英]java to C/C++(carbide c/c++) converstion

In Java : 在Java中:

value = 1122; 值= 1122;

public static final byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
    }

  public static int byteArrayToInt(byte[] data) {
        return (int)(
                (int)(0xff & data[0]) << 24  |
                (int)(0xff & data[1]) << 16  |
                (int)(0xff & data[2]) << 8   |
                (int)(0xff & data[3]) << 0
        );
    }

here it'll return [0, 0, 4, 98] so in C: 这里它将返回[0,0,4,98]所以在C中:

char* intToByteArray(int value){
        char* temp = new char[4];
         temp[0] = value >> 24,
         temp[1] = value >> 16,
         temp[2] = value >> 8, 
         temp[3] = value;
         return temp;
} 

since there's no byte data type in c we can use char* instead of that but when i return temp value i am getting null so i checked values like this where = b\\x04 'b'= 98, x04 = 4 i am not able to get data which is zero so while converting back how should i manage remaining values?? 因为在c中没有字节数据类型我们可以使用char *而不是那个但是当我返回临时值时我得到null所以我检查了这样的值where = b \\ x04'b'= 98,x04 = 4我不能获取数据为零所以在转换回来时应该如何管理剩余价值?

         char* where = new char[10];
         where[0] = temp[3];
         where[1] = temp[2];
         where[2] = temp[1];
         where[3] = temp[0];
         where[4] = 0;

i am getting null 我变得空了

No you are not. 不,你不是。 You are getting a first byte which is a \\0 nul byte. 你得到的第一个字节是\\0 nul字节。 When you print this as a text string it will terminate the string. 当您将其打印为文本字符串时,它将终止字符串。 But its not a string, its an array of bytes. 但它不是一个字符串,它是一个字节数组。

If you were getting a NULL, you would get a segmentation fault or the like. 如果您获得NULL,则会出现分段错误等。

While the above post is absolutely correct, you have one more serious problem with your porting effort. 虽然上面的帖子绝对正确,但您的移植工作还有一个严重的问题。 Your java produces 你的java产生

[ 0, 0, 4, 98 ]

while your C produces 而你的C产生

[ 98, 4, 0, 0 ]

The code won't work when simply converted like that. 简单地转换后代码将不起作用。 Java is big-endian regardless of the hardware, but your C(++) unsurprisingly is low-endian. 无论硬件如何,Java都是big-endian,但不出所料,你的C(++)是低端的。 Worse still, you cannot be sure that it will be that way every time. 更糟糕的是,你不能确定它每次都会那样。 With Carbide, i guess that you can perfectly stumble upon a big endian architecture (some ARMs? not an expert in this field). 有了Carbide,我猜你可以完美地发现一个大端架构(一些ARM?不是这个领域的专家)。 So you must either detect endian-ness of your Carbide platform or, replace the bitshift with an incremental modulo 256. Depends on how often you call the function. 因此,您必须检测Carbide平台的endian-ness,或者用增量模256替换bitshift。取决于您调用该函数的频率。 Modulo will take seriously more processor effort but doesn't care about endianness. Modulo将需要更多的处理器工作,但不关心字节序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM