0

I need to find out the length of a string for different character sets in Java. Assuming that I have 160 characters and the ASCII character set, the byte length should be 140. Similarly I need for other character set also.

I may get an input stream in any of the character set, but once received it will be stored in UTF-16 and subsequently I will not be able to get the exact byte length of original character set.

2
  • 4
    You could convert it to bytes and just invoke .length? Commented Sep 4, 2014 at 7:24
  • There you are, @Duncan Commented Sep 4, 2014 at 7:44

2 Answers 2

1

Refer to this answer to convert the String to bytes (tl;dr use .getBytes()) and then invoke the .length property to get the number of bytes in the String.

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

Comments

0

Above solution is not suitable for handling 7 bit ASCII character set or UTF-7 . In UTF-7 every characters in the stream represented in 7 bits not 8 bits. so we can send 160 characters in 140 bytes using UTF-7 Character Encoding . But while using getBytes("UTF-7").length function on inputString of 160 characters we get 160 Bytes instead of 140 Bytes.

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.