I need to save a variable on a JavaCard. Javacard's don't support Strings, so I have to hardcode some String variables as byte arrays.
Unfortunately, I don't know how to achieve this format:
new byte[]{0x4A, 0x61, 0x6E, 0x20, 0x56, 0x6F, 0x73, 0x73, 0x61, 0x65, 0x72, 0x74};
Is there an online tool available? Or is there a program that output's it that way so I can copy paste the output and use that for hardcoding?
You don't need any tool for that. If you want to store an string in your applet in the applet developing step (I mean in the programming phase) use a byte array as below :
public static byte[] myFiled = {(byte)'T', (byte)'E', (byte)'S', (byte)'T'};
or use Hex values instead of the letters:
public static byte[] myFiled = {(byte)0x10, (byte)0x11, (byte)0x12, (byte)0x13};
It's necessary to cast the array elements to byte explicitly
And if you want to store the string after developing in installing your applet, first convert it to its hex value using this online tool for example, and then send it to card in the data field of an APDU command. And then using arrayCopy or arrayCopyNonAtomic methods store it in you byte array.
Just use String::getBytes():
String a = "HelloWorld";
byte[] inBytes = a.getBytes();
System.out.println(Arrays.toString(inBytes));
OUTPUT:
[72, 101, 108, 108, 111, 87, 111, 114, 108, 100]
IDEONE DEMO
ADD ON: as #AndyTurner mentioned, you can specify charset using String::getBytes(Charset).
Find here a nice explanation.
Here's a method to convert a string to an array literal that represents US-ASCII–encoded bytes.
static String format(String str)
{
byte[] encoded = str.getBytes(StandardCharsets.US_ASCII);
return IntStream.range(0, encoded.length)
.mapToObj(idx -> String.format("0x%02X", encoded[idx]))
.collect(Collectors.joining(", ", "{ ", " }"));
}
Because it uses ASCII, the high bit of each byte is zero, and no downcasts to byte are required.
Related
I am a beginner in java I want to convert String to a byte array and vice versa. when I compare the result input_bytes with output.getBytes(). I found that they are not compatible. this is my code.
String input = "bg#%#bg0";
byte[] input_bytes = input.getBytes();
String output = new String(input_bytes);
System.out.println(input_bytes);
System.out.println(output.getBytes());
the result :
[B#15db9742
[B#6d06d69c
How can I get the same byte array from input and output? and what is the problem in my code?
Try:
System.out.println(Arrays.toString(input_bytes));
You need to use Arrays.toString() method.
Output: [98, 103, 64, 37, 64, 98, 103, 48]
Note: Here Arrays.toString(byte[]) returns a string representation of the contents of the specified byte array.
I am using a Android Phone to communicate with a BLE device.
The method to send data for the library needs byte[],
sharing one of the static example snippet:
public static final byte dataRequest[] = { 0x23, 0x57, 0x09, 0x03, (byte) 0xD4};
sendDataToDevice(dataRequest);
The data i am receiving from the user is in String, for example
String str1 = "D4";
now my question is , how to convert this String value (which is actually a hex value in String datatype) to byte, so that i can store these dynamic String values and convert and then insert it into byte[] like ,
byte[0] = convertToByte(str1);
where byte[0] must store value as 0xD9 or like the format given in static example.
You should just be able to use Integer#parseInt with a radix of 16 (hexadecimal) to convert a String to an int (which you can then cast to a byte and store in your array):
String str1 = "D4";
byte b = (byte) Integer.parseInt(str1, 16);
System.out.println(b);
Output:
-44
Note: Byte#parseByte can't be used in your example, as Byte#parseByte uses Integer#parseInt internally and parses D4 as 212, which is not a valid value for a signed byte.
I have to convert an existing Java code for encryption to PHP.
I am stuck in the below Java code where the key is converted to a byte array using Java's BigInteger class
private static byte[] stringToBytes(String str){
byte[] bytes=new BigInteger(str, Character.MAX_RADIX).toByteArray();
return Arrays.copyOfRange(bytes, 1, bytes.length);
}
I am able to convert the input string to big int using
base_convert($key,36,10);
But cannot convert to byte array.
Any inputs please
A sample output for Java code is shown below
stringToBytes("TestString")
gives
[10, -100, -89, -107, 122, -108, 44]
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));
}
For testing purposes, I tried to create an array like this:
byte[] expected = new byte[]{0x2f, 0x0d4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc}
I expected, that java will complain and will ask me to cast every literal here to (byte), but unexpectedly, it asked me only to convert 0x4d, for example, but not 0x2f. The working example:
new byte[]{0x2f, (byte) 0xd4, (byte) 0xe1, (byte) 0xc6, 0x7a, 0x2d, 0x28, (byte) 0xfc}
How does that work?
I suspect it is because the Java byte is signed, thus you have a range between -128 and 127. So all values >127 (0x80) have to be explicitly converted.
An number literal without a l, d or f is an int value, so values 0x80 and larger have to be cast. One way to cover lots of hex values is to use the following
byte[] bytes = new BigInteger("2fd4e1c67a2d28fc", 16).toByteArray();
System.out.println(Arrays.toString(bytes));
prints
[47, -44, -31, -58, 122, 45, 40, -4]
This avoids some of the tedious , (byte) 0x between values.
Integer literal between -128 to 127 will be automatically converted into target type and Java has signed types only.