I want to convert byte data in Java to byte in C.
a is int and b is byte; and I have code in Java like this:
b[0x00] = (byte) ((a>> 8) & 0xFF);
how can I convert above statement in C?
Actually this is virtually equivalent in C as both the >> and & operators are identical in both languages.
int a=2200;
unsigned char b=((a>>8) & 0xFF);
Related
I'm trying to convert an int (max. 65535) to an two bytes array.
In C I used an uint16, but Java doesn't know any unsigned values.
To convert the bytes to an array I tried to use this:
byte[] data = { (byte) ((num >> 8) & 0xff), (byte) (num & 0xff) };
But I only get: [63, -49] instead of: [63, 207], if I use 16335 as value.
Is there a way to do this in Java?
I need this unsigned byte in an byte array to send it using an outputstream
You can use java's NIO's ByteBuffer for the purpose:
byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();
for (byte b : bytes) {
System.out.println(Byte.toUnsignedInt(b));
}
Hope now it would work ;)
How would one concatenate two byte primitives to get one char in java? I'm currently trying this:
byte a = 0x01;
byte b = 0x02;
char c = (char) (a + b);
which gives 0x03. The answer I want is 0x0102.
Is there no simple way to concatenate primitives? I'm surprised there isn't an obvious solution, since it seems like it should be easy. Maybe there is and I just don't see it. :p
Any help would be appreciated.
Thanks!
By shifting left 8 bits and adding. Something like,
byte a = 0x01;
byte b = 0x02;
char c = (char) ((a << 8) + b);
System.out.println(Integer.toHexString(c));
Output is
102
As pointed out in the comments (by #ChrisMartin) you could bitwise-or (and I'll add you might also xor) like
char c = (char) ((a << 8) | b);
and
char c = (char) ((a << 8) ^ b);
To all achieve the same result. Since you should Write Dumb Code I suggest you use whichever you find easiest to understand.
If you are not opposed to using a 3rd party library, Guava's Chars.fromBytes method does exactly this:
char c = Chars.fromBytes(0x01, 0x02);
The correct expression is ((a << 8) | (b & 0xff)):
byte a = (byte) 0x81;
byte b = (byte) 0x82;
char c = (char) ((a << 8) | (b & 0xff));
A byte, when used in an expression, is promoted to an int and when this is done, it is sign-extended. The expressions in the other answer only worked when b <= 127.
If you add 1 more to that value, as a byte, it will have the value -128. (The range of a byte in Java is -128 to 127) so you usually have to mask a byte with b & 0xff when you're using it to store unsigned numbers. You don't need to do that with a in the above expression since all the high-order bits are shifted out of the resulting 16-bit char.
This does not seem to be appropriate. Is there a way to create a hexadecimal array?
float[] bitBytes = {0x80, 0x40, 0x20, 0x10, 8, 4, 2, 1};
for (int k = 0; k < alot; k++) {
BitSet.set(increment++, ((array[k] & (bitBytes[k%8]& 0xff)) != 0));
}
Hexadecimals is a representation of bytes as a String, or at least an array of characters. It is mainly used for human consumption, as it is easier to see the bit value of the bytes.
To create a byte array containing byte values, you can use the following construct:
final byte[] anArray = { (byte) 0x10, (byte) 0x80 };
The cast to byte - (byte) - is really only required for values of 0x80 or over as bytes are signed in Java and therefore only have values ranging from -0x80 to 0x7F. Normally we only deal with unsigned values though, so we need the cast.
Alternatively, for larger strings, it can be useful to simply supply a hexadecimal string to a decoder. Unfortunately the idiots that have thought out the standard API still haven't defined a standard hexadecimal codec somewhere in java.lang or java.util.
So you can use another library, such as the Apache codec library or a self written function. Stackoverflow to the rescue.
Convert a string representation of a hex dump to a byte array using Java?
If you want to have a BitSet of the values in the byte array, please use BitSet.valueOf(byte[])
byte[] biteBytes = new byte[8];
for (int j = 0; j < bitBytes.length; j++) {
bitBytes[j] = (byte) (Math.pow(2,j));
}
I have some C++ code I'm trying to port to Java, that looks like this:
struct foostruct {
unsigned char aa : 3;
bool ab : 1;
unsigned char ba : 3;
bool bb : 1;
};
static void foo(const unsigned char* buffer, int length)
{
const unsigned char *end = buffer + length;
while (buffer < end)
{
const foostruct bar = *(reinterpret_cast<const foostruct*>(buffer++));
//read some values from struct and act accordingly
}
}
What is the reinterpret_cast doing?
its basically saying the 8 bits represented at the current pointer should be interpreted as a "foostruct".
In my opinion it would be better written as follows:
const unsigned char aa = *buffer & 0x07;
const bool ab = (*buffer & 0x08) != 0;
const unsigned char ba = (*buffer & 0x70) >> 4;
const bool bb = (*buffer & 0x80) != 0;
I think it is far more obvious what is being done then. I think you may well find it easier to port to Java this way too ...
It does what a classic C-style (const foostruct *)buffer would do at worst: tells C++ to ignore all safety and that you really know what you are doing. In this case, that the buffer actually consists of foostructs, which in turn are bit fields overlain on single 8 bit characters. Essentially, you can do the same in Java by just getting the bytes and doing the shift and mask operations yourself.
you have a pointer to unsigned char right? Now imagine that the bits pointed to by the pointer are treated as though it were an object of type foostruct. That's what reinterpret_cast does - it reinterprets the bit pattern to be a memory representation of another type...
since I need to control some devices, I need to send some bytes to them. I'm creating those bytes by putting some int values together (and operator), creating a byte and finally attaching it to a String to send it over the radio function to the robot.
Unfortuantely Java has some major issues doing that (unsigned int problem)
Does anybody know, how I can convert an integer e.g.
x = 223;
to an 8-bit character in Java to attach it to a String ?
char = (char)x; // does not work !! 16 bit !! I need 8 bit !
A char is 16-bit in Java. Use a byte if you need an 8-bit datatype.
See How to convert Strings to and from UTF8 byte arrays in Java on how to convert a byte[] to String with UTF-8 encoding.
Sending a java.lang.String over the wire is probably the wrong approach here, since Strings are always 16-bit (since Java was designed for globalization and stuff). If your radio library allows you to pass a byte[] instead of a String, that will allow you to send 8-bit values without needing to worry about converting to UTF8. As far as converting from an int to an unsigned byte, you'll probably want to look at this article.
int to array of bytes
public byte[] intToByteArray(int num){
byte[] intBytes = new byte[4];
intBytes[0] = (byte) (num >>> 24);
intBytes[1] = (byte) (num >>> 16);
intBytes[2] = (byte) (num >>> 8);
intBytes[3] = (byte) num;
return intBytes;
}
note endianness here is big endian.