0

My question is if there is any short way to convert a String to a byte-Array using standard java-library-functions? The "string" does have NO ENCODING, the string itself is a string for no reason, but it is the way I get it. I did not code it myself and I cannot change it, but I have to deal with it. A standard String.getBytes() will not work because some values always get "?"-ed out and make the underlying binary-data obviously useless.

Any ideas?

It's basically the same question as here: How do I get a consistent byte representation of strings in C# without manually specifying an encoding? but instead of C# for Java. I searched quite some time but I couldn't find any fitting solution.

Thanks in advance.

Community
  • 1
  • 1
SkryptX
  • 570
  • 1
  • 5
  • 19
  • If you can show the string you are dealing with then we might be able to help. Also if it has no encoding why are some bytes ?ed out. What OS are you running on? – Himanshu Bhardwaj Jan 12 '17 at 12:32
  • Relevant? http://stackoverflow.com/questions/23316755/java-string-getbytes-charsetname-vs-string-getbytes-charset-object – Tim Biegeleisen Jan 12 '17 at 12:33
  • Please share a string that gives '?' while converting. Most probably the issue with the Character Encoding, Please try with 'UTF-8' – Abin Manathoor Devasia Jan 12 '17 at 12:34
  • It is not possible to "give a string" that generates a "?"... Typical hex-values that get converted to 0x3F("?") are 0x8E or 0x8F and others that are not printable characters in virtually any charset(either not used or control characters) – SkryptX Jan 12 '17 at 12:39

2 Answers2

1

Unfortunately, there is no such thing as a "String without encoding" in Java. As the documentation says:

A String represents a string in the UTF-16 format

So the moment you get the data as a String it is already encoded and there is no way around it.

lukeg
  • 3,489
  • 3
  • 15
  • 37
1

In Java, String is a wrapper around an array of char, nor byte. Moreover, the strings are immutable, so direct access to underlying char[] is not allowed.

The closest things to direct access are:

  • make a copy of char array
char[] toCharArray() 

Converts this string to a new character array.

and

  • get the character at index
char charAt(int index)

Returns the char value at the specified index.