In C there is an escape sequence that lets you embed a hex (or binary) value into a string? Something like this.
String str = "12345" + "\x0B" + "Some additional text";
Is there a similar function in Java?
It's not clear what you're trying to achieve. A String in Java is just a sequence of UTF-16 code units (which usually means "a sequence of characters", but characters outside the Basic Multilingual Plane require two UTF-16 code units). If you need binary data in there as well, you shouldn't use a string.
Now you can include the Unicode character U+000B (line tabulation) in the string, using \u000b - but you need to make sure that's actually what you want to do.
If you're actually trying to mix text data and binary data, I'd encourage you not to do that - it's very easy to lose data or convert it badly. If you provide your actual requirements, we may be able to help you come up with a much better solution.
\u000b". The number of years you've been doing something doesn't make you smarter it seems :)In Java you can use Integer.toString(int i, int radix).
For hexadecimal, this is
String str = "12345" + Integer.toString(0x0B, 16) + "Some additional text";
As already stated, Java Strings are Unicode, therefore 2 bytes, and is represented internally as a char[].
@Jon Skeet is right saying "If you need binary data in there as well, you shouldn't use a string" (but then Jon Skeet is always right ;)): you should use a byte[].
However, if you really need to use a String to store binary data (because it sometimes is out of your hands), a Charset will encode your String into a byte[]. And it turns out the ISO-8859-1 charset will map a char to a single byte:
String str = "aéç";
System.out.println(Arrays.toString(str.getBytes(StandardCharsets.ISO_8859_1)));
System.out.println(Arrays.toString(str.getBytes(StandardCharsets.UTF_8)));
System.out.println(Arrays.toString(str.getBytes(StandardCharsets.UTF_16)));
gives:
[97, -23, -25]
[97, -61, -87, -61, -89]
[-2, -1, 0, 97, 0, -23, 0, -25]
So you coud hack your way through it using the String(byte[], Charset) constructor to store binary data:
String str = "12345_Some additional text"; // '_' for the replacement character
byte[] hack = str.getBytes(StandardCharsets.ISO_8859_1);
hack[5] = 0x0B; // Change it here
str = new String(hack, StandardCharsets.ISO_8859_1); // Put it back