I want to put a string value to an array byte without any conversion.
the string length is always 14 and it's in fact an hexadecimal value presenting the date and time. so every 2 chars in this string is a byte so my byte array is a 7 bytes length.
i didn't find a way to do so.
the string.getBytes()
method is doing a conversion.
i tryed this method also https://stackoverflow.com/a/11208685/3343174 but after debugging it's showing a different values from the first string.

- 1
- 1
-
Provide an example and what you have tried. – Jens Jul 31 '15 at 10:34
-
You will need to design a loop that iterates over the characters of your `String`. Once you have some code ready and if you run into trouble, then the community will help you fix it. – dotvav Jul 31 '15 at 10:34
-
i tryed string.getBytes(); and i tryed another method that i found in this answer: http://stackoverflow.com/a/11208685/3343174 but all of them are doing a conversion !! – Jul 31 '15 at 10:38
-
@SaHa what to you mean by conversion? Surely that is exactly what you want to do. Otherwise, for example, `A` has a byte value of `65`. You understand that the numeric value of `A` as a hexadecimal digit is different to the value of `A` is a `char`? – Boris the Spider Jul 31 '15 at 10:41
-
http://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html#Byte(java.lang.String) ?? You can split your String into pieces of 2 and use this to get the corresponding byte-value. Then put it in an array. – Fildor Jul 31 '15 at 10:41
-
@Fildor how can i call this method ?!! – Jul 31 '15 at 10:45
-
@BoristheSpider for example the first four chars in my string are '07df' i want that the 2 first bytes in my byte array contain respectively '07' and 'df' without converting them to another values – Jul 31 '15 at 10:52
-
Actually, my comment was wrong. You need Byte.parseByte( string, 16 ) to parse Hex. Byte(string) would expect radix 10. You can loop around that method and use String.substring to "cut out" the relevant chars from your string. – Fildor Jul 31 '15 at 10:57
-
Turning a `char` into a `byte` requires a conversion. Are you saying that "07df" is 4 characters of hexidecimal and you need to turn this into two bytes? This is a conversion. – Peter Lawrey Jul 31 '15 at 11:07
-
@Fildor that's what i did now. It's generating this exception : Caused by: java.lang.NumberFormatException: Invalid int: "" at this instruction : result[i] = Byte.parseByte(subS,16); – Jul 31 '15 at 11:08
-
@PeterLawrey the problem is when i use any of methods provided by JAVA, the value "07df" is changing !! – Jul 31 '15 at 11:09
2 Answers
You can get a substring of two chars of your string like this:
String yourString = "07df";
String firstByteAsString = yourString.substring(0,2); // start - end(excl.)
// = "07"
You can get a byte from this like this:
byte b = Byte.parseByte( firstByteAsString, 16 );
I am sure, you'll manage to use this to get your desired functionality.
EDIT As Peter points out: Values > 127 are a Problem. So you'll actually have to use
byte b = (byte) (Integer.parseInt( firstByteAsString, 16) & 0xFF);
But his answer is much simpler and doing all this for you. You should go for BigInteger and accept Peter's answer.

- 14,510
- 4
- 35
- 67
-
It's generating this exception : Caused by: java.lang.NumberFormatException: Invalid int: "" at this instruction : result[i] = Byte.parseByte(firstByteAsString ,16); – Jul 31 '15 at 11:12
-
Can you update the question with your new code and complete stacktrace of the error, please? – Fildor Jul 31 '15 at 11:13
-
2It think he is trying to say that `df` is > 127 and cannot be parsed as a byte. – Peter Lawrey Jul 31 '15 at 11:14
-
-
-
@PeterLawrey oh, yeah. I didn't think of that. I +1ed your answer. I better have a look at the BigInteger again. That's one neat solution. – Fildor Jul 31 '15 at 11:17
-
Ok this is the aswer but the problem is that the value F is out of range !! what must i do now ?? – Jul 31 '15 at 11:22
-
@SaHa The simplest thing is to use BigInteger. It works for any size of string. – Peter Lawrey Jul 31 '15 at 11:24
-
@SaHa The Problem is that there is no "unsigned" in Java. So byte is not 0 to 255 but -128 to +127 and df is bigger than 127 and thus out of range. So actually you'd have to parse to Integer and then mask it with 0xFF to get rid of the sign. Peter's answer with BigInteger is doing all this for you. – Fildor Jul 31 '15 at 11:32
-
@Fildor the `& 0xFF` is needed to turn signed into unsigned. To go from unsigned to signed the cast to `(byte)` is enough. – Peter Lawrey Jul 31 '15 at 11:41
-
1@PeterLawrey I thought he wanted to "stay" unsigned but I think, I misunderstood what was simply a matter of "print-out" interpretation of the value. – Fildor Jul 31 '15 at 11:51
Lets assume you want to convert a hexidecimal string into byte[]. e.g. you have 14 characters in hexidecimal and you want to convert this to bytes where two character represent each byte.
for example the first four chars in my string are '07df' i want that the 2 first bytes in my byte array contain respectively '07' and 'df'
String base16 = "07df07df07df07";
byte[] bytes = new BigInteger(base16, 16).toByteArray();
if (bytes[0] == 0)
bytes = Arrays.copyOfRange(bytes, 1, bytes.length);
System.out.println(Arrays.toString(bytes));
prints
[7, -33, 7, -33, 7, -33, 7]
I want to get [07, df, 07, df, 07, df, 07]
This is a matter of formatting the byte[]
in the form you want. (byte) 0xDF
is -33
Numbers are just numbers, they don't remember the format you used to create them, they just have one default format and if you don't like, it you can add your own.
StringBuilder sb = new StringBuilder();
String sep = "[";
for (byte b : bytes) {
sb.append(sep).append(String.format("%02x", b & 0xFF));
sep = ", ";
}
sb.append("]");
System.out.println(sb);
prints
[07, df, 07, df, 07, df, 07]

- 525,659
- 79
- 751
- 1,130
-
i want to get [07, df, 07, df, 07, df, 07] another thing i tryed the answer of @Fildor but the problem is that the value df is out of range and it's generating the exception : Caused by: java.lang.NumberFormatException: Value out of range for byte: "f0" – Jul 31 '15 at 11:28
-
@SaHa `(byte) 0xdf` is -33 if you can change the way it is printed if you want hexidecimal. – Peter Lawrey Jul 31 '15 at 11:36
-
that's my main problem. i don't want that the value is converted !! i want to get the df value not the conversion of it to a decimal value – Jul 31 '15 at 13:07
-
2@SaHa **all numbers are just numbers**. They are not decimal, they are not hexadecimal. They are **binary numbers**. If you want to **print** a number then you need to **decide on a format** _when it is printed_. – Boris the Spider Jul 31 '15 at 13:08
-
1@BoristheSpider ok i get it. so if i pass the "-33" value is the same if i pass "df" ?? i'm developing an application that use Bluetooth low energy and i must pass a byte[] parameter to the method so i can send a value. – Jul 31 '15 at 13:10
-
@SaHa I have demonstrate how to print it in the manner you wish but a byte is just 8-bits. What it means to you depends on what you need it to mean. In Java it defaults to an unsigned byte, but you can make it any 256 values you want. – Peter Lawrey Jul 31 '15 at 17:41
-
1i resolve the problem. even when i pass value converted the other device will know it!! they are all the same :) – Aug 01 '15 at 11:14
-
@SaHa Technically, the device has no idea what the 8-bit meant to you when you wrote them. It could be signed, unsigned, text characters, originally decimal, hexidecimal, binary. This information is recorded anywhere. The device just assumes the values are in the form it needs. – Peter Lawrey Aug 01 '15 at 14:30