Int to byte array - byte shifting - java

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

Related

Convert int to hex byte value

I want to read block wise data of NFC tag. For which the command is a byte array and it needs block number.
public static byte[] readSingleBlockCmd(int blockNo) {
byte[] cmd = new byte[3];
cmd[0] = (byte) 0x02;//flag
cmd[1] = (byte) 0x23;//cmd
cmd[2]= (byte)blockNo;
return cmd;
}
How can I change the int blockNo to its hexadecimal value , which can be cast to byte .I want the byte value and not an byte []
I have gone through the following links
Convert integer into byte array (Java)
How to autoconvert hexcode to use it as byte[] in Java?
Java integer to byte array
Thanks!
Converting an integer (in decimal) to hex can be done with the following line:
String hex = Integer.toHexString(blockNo);
Then to convert it to byte, you can use
Byte.parseByte(hex,16);
But if all you wanted to do is convert the parameter to bytes:
Byte.parseByte(blockNo);
would work too I guess. Correct me if I'm wrong.

I want to convert byte data in Java to C

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

How to create a Java Hexadecimal Array

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

Java SHA-1 hash an unsigned BYTE

Hy guys!
I have the following problem:
I need to hash an unsigned byte in Java which is(would be...) between 0-255.
The main problem is that java doesnt have an unsigned Byte type at all.
I found a workaround for this, and used int instead of byte with a little modification.
The main problem is: Java.securitys Messagedigest.digest function only accepts byte array types, but i would need to give it an int array.
Anybody has a simpe workaround for this?
I was looking for a third party sha-1 function, but didnt found any. Nor any sample code.
So basically what i need:
I have an unsigned byte value for example: 0xFF and need to get the following sha1 hash: 85e53271e14006f0265921d02d4d736cdc580b0b
any help would be greatly appreciated.
It's important to understand that there is no difference between signed and unsigned bytes with respect to their representation. Signedness is about how bytes are treated by arithmetic operations (other than addition and subtraction, in the case of 2's complement representation).
So, if you use bytes for data storage, all you need is to make sure that you treat them as unsigned when converting values to bytes (use explicit cast with (byte), point 1) and from bytes (prevent sign extension with & 0xff, point 2):
public static void main(String[] args) throws Exception {
byte[] in = { (byte) 0xff }; // (1)
byte[] hash = MessageDigest.getInstance("SHA-1").digest(in);
System.out.println(toHexString(hash));
}
private static String toHexString(byte[] in) {
StringBuilder out = new StringBuilder(in.length * 2);
for (byte b: in)
out.append(String.format("%02X", b & 0xff)); // (2)
return out.toString();
}
Look at Apache Commons Codec library, method DigestUtils.sha(String data). It may be useful for you.
The digest won't care about how Java perceives the sign of a byte; it cares only about the bit pattern of the byte. Try this:
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update((byte) 0xFF);
byte[] result = digest.digest();
StringBuilder buffer = new StringBuilder();
for (byte each : result)
buffer.append(String.format("%02x", 0xFF & each));
System.out.println(buffer.toString());
This should print 85e53271e14006f0265921d02d4d736cdc580b0b.

Convert INT(0-255) to UTF8 char in Java

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.

Categories