Converting bytearray to Objective-C data structure [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Byte array in objective-c
Am converting some Java code to Objective-C and have run into an issue that I can't get my head around:
public static final byte[] DATA_GENERIC = new byte[] { (byte)0xA0, 0x00, 0x00, 0x00, 0x03,
0x10, 0x10 };
Does anyone know to convert the above into Objective-C

Here is an example of getting your data into a NSData object.
const unsigned char bytes[] = { 0xA0, 0x00, 0x00, 0x00, 0x03, 0x10, 0x10 };
NSData *data = [NSData dataWithBytes:bytes length:7];
NSLog(#"%#", data);
Output:
<a0000000 031010>
One major difference from java is you will need to keep track of the number of bytes yourself when working with a raw char array. Once you create the NSData you can access the length.

Related

Weird "illegal start of expression" error in Netbeans

In the following programs, command is a byte array field in Apdu class. The problem is that my IDE (Netbeans) mark the line of
apdu.command = {(byte) 0x00, (byte)0xa4, (byte) 0x00, (byte) 0x00};
As error, with the message illegal start of expression, while it is okay to use the following instead:
byte[] bytes = {(byte) 0x00, (byte) 0xa4, (byte) 0x00, (byte) 0x00};
apdu.command = bytes;
What's wrong with the first programs?
First program:
Second Program:
You need to initialize the array variable with :
apdu.command = new byte[] {(byte) 0x00, (byte)0xa4, (byte) 0x00, (byte) 0x00};
The initialization you tried is only valid in array declaration expressions.
array syntax {} can only be used for initialization not for assignment.
you need to use apdu.command =new byte[] {(byte) 0x00, (byte)0xa4, (byte) 0x00, (byte) 0x00};.

How to avoid individual casting to byte during initialization of byte array in Java

I am using a byte array to store a text file outside of the filesystem.
It looks like this:
private static final byte[] CDRIVES = new byte[] {
(byte)0xe0, 0x4f, (byte)0xd0, 0x20, (byte)0xea, 0x3a, 0x69, 0x10,
(byte)0xa2, (byte)0xd8, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte)0x9d,
(byte)0xba, (byte)0x8a, 0x0d, 0x45, 0x25, (byte)0xad, (byte)0xd0, 0x11,
(byte)0x98, (byte)0xa8, 0x08, 0x00, 0x36, 0x1b, 0x11, 0x03,
(byte)0x80, 0x53, 0x1c, (byte)0x87, (byte)0xa0, 0x42, 0x69, 0x10,
(byte)0xa2, (byte)0xea, 0x08, 0x00, 0x2b, 0x30, 0x30, (byte)0x9d
...
...
...
};
Is there a way to avoid casting to (byte) for better visual interpretation?
I don't mind using other data type, but I need to be able build an InputStream out of it and do it the fastest way if possible. (for example storing a text file into a String variable is not the best way...)
Well one simple approach would be to use base64 - but perform the conversion on class initialization, so you only take the performance hit once:
private static final byte[] CDRIVES = Base64.decode("YOURBASE64HERE");
Or if it's genuinely text, perform that encoding once in a similar way:
private static final byte[] CDRIVES =
"Your constant text here".getBytes(StandardCharsets.UTF_8);
Again, the performance hit occurs exactly once, and then you can use the bytes multiple times. I would be very surprised if the cost of encoding text into bytes at class initialization time is genuinely a bottleneck for you.

possibly lossy conversion from int to byte

I am trying to write hexadecimal data into my serial port using java, but now I cant convert the hexadecimal data into the byte array.
Here is the code which shows the error message:
static byte[] bytearray = {0x02, 0x08, 0x16, 0x0, 0x00, 0x33, 0xC6, 0x1B};
This is the code writing into the serial port:
try {
outputStream = serialPort.getOutputStream();
// Write the stream of data conforming to PC to reader protocol
outputStream.write(bytearray);
outputStream.flush();
System.out.println("The following bytes are being written");
for(int i=0; i<bytearray.length; i++){
System.out.println(bytearray[i]);
System.out.println("Tag will be read when its in the field of the reader");
}
} catch (IOException e) {}
Can I know how can I solve this problem. Currently I am using the javax.comm plugin. Thank you.
If you look at the error message:
Main.java:10: error: incompatible types: possible lossy conversion from int to byte
static byte[] bytearray = {0x02, 0x08, 0x16, 0x0, 0x00, 0x33, 0xC6, 0x1B};
^
There is a small caret pointing to the value 0xC6. The reason for the issue is that java's byte is signed, meaning that its range is from -0x80 to 0x7F. You can fix this by casting:
static byte[] bytearray = {0x02, 0x08, 0x16, 0x0, 0x00, 0x33, (byte) 0xC6, 0x1B};
Or, you can use the negative, in-range value of -0x3A (which is equivalent to 0x36 in two's-complement notation).
Try to cast 0xC6 like this as byte range is from -0x80 to 0x7F:
static byte[] bytearray = {0x02, 0x08, 0x16, 0x0, 0x00, 0x33, (byte) 0xC6, 0x1B};

Bluetooth LE Java byte array size on characteristic setValue

I am trying to send a value using Bluetooth LE on my Android phone with the following line of code.
I am getting an error that it exceeds the size of the array, which is 127 because of the 0xEA byte. I converted the byte to around 234. Is there a way to send this byte using the following line of code?
private void writeCharacteristic(BluetoothGatt gatt)
{BluetoothGattCharacteristic characteristic;
Log.d(TAG, "Writing Data");
characteristic = gatt.getService(SERVICE).getCharacteristic(DATA_IN);
characteristic.setValue(new byte[]{0x08, 0x01, 0x03, 0x04, 0x52, 0x00, 0x02, 0x62, 0xEA});
gatt.writeCharacteristic(characteristic);
}
To be able to use byte values above 127 in java, use (byte)0xEA.

How to convert a "binary string" to base64?

I've followed this: https://dev.twitter.com/docs/auth/creating-signature
to the end but I can't find how to encode a "binary string" to base64(at the end). I wanted to try online converters first but they don't give the string the show
"tnnArxj06cWHq44gCs1OSKk/jLY="
Tried this: http://www.motobit.com/util/base64-decoder-encoder.asp
and
http://www.hash-cracker.com/base64.php#anchor
and
http://www.opinionatedgeek.com/dotnet/tools/Base64Encode/
and none give that string.
I'm going to be using java. But I think all those java tools I search for will give the same result as the online converters. What has to be done to encode a "binary string" to base64?
The problem of using those online tools isn't going to be in the base64 conversion - it's going to be parsing the hex string into a byte array to start with. In your real code that won't be a problem, as it'll be the output of another stage. Just to prove that, here's some sample Java code, using a public domain base64 encoder:
public class Test {
public static void main(String[] args) throws Exception {
byte[] data = { (byte) 0xB6, (byte) 0x79, (byte) 0xC0, (byte) 0xAF,
(byte) 0x18, (byte) 0xF4, (byte) 0xE9, (byte) 0xC5,
(byte) 0x87, (byte) 0xAB, (byte) 0x8E, (byte) 0x20,
(byte) 0x0A, (byte) 0xCD, (byte) 0x4E, (byte) 0x48,
(byte) 0xA9, (byte) 0x3F, (byte) 0x8C, (byte) 0xB6 };
String text = Base64.encodeBytes(data);
System.out.println(text);
}
}
Output: tnnArxj06cWHq44gCs1OSKk/jLY=
You can use this link to convert Binary String to Base64
https://cryptii.com/pipes/binary-to-text
Here is an example of converting Binary String to Base64 String:
Here is another example of converting Normal String to Base64 String

Categories