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.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • 4
    You could [convert it to bytes](http://stackoverflow.com/questions/5688042/how-to-convert-a-java-string-to-an-ascii-byte-array) and just invoke .length? – hd1 Sep 04 '14 at 07:24
  • There you are, @Duncan – hd1 Sep 04 '14 at 07:44

2 Answers2

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.

Community
  • 1
  • 1
hd1
  • 33,938
  • 5
  • 80
  • 91
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.