9

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?

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Jon: I'll accept that your not supposed to embed Binary into a Java String, and know well what problems it can cause from 30 years++ of embedded systems programing but in this case it would have been a convenient solution. I'll just rewrite the code to treat the file as a binary file with embedded ASCII characters - Joe
@JoeCullity Sorry, but saying you have "30 years++ of [experience]" sounds quite childish given the fact that you're asking a basic question and you don't seem to have understood the basic answer of "just use \u000b". The number of years you've been doing something doesn't make you smarter it seems :)
5

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

Comments

1

In Java, Strings are Unicode text. They cannot embed arbitrary binary data. If you want, you can embed the Unicode U+000B LINE TABULATION character though:

String str = "12345\u000BSome additional text";

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.