• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ascii to binary character replacement....  RSS feed

 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to replace the following in a string. Can someone tell me how best to accomplish this?

* Whitespace is discarded (unless preceded by a backslash)
* '\e' is translated to ASCII ETX (0x03)
* '\n' is translated to ASCII LINEFEED (0x0A)
* '\s' is translated to ASCII STX (0x02)
* '\x' is translated to ASCII ESCAPE (0x1A)

Thanks!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Strings are in Unicode. Ordinary printable ASCII characters can be converted to 16-bit Unicode representation by just extending the 7-bit ASCII value to 16-bits, by adding zeros. I am not sure whether that applies to all the control characters you have mentioned. If it does, then your ASCII characters can be represented by \U00xx escapes in Java, where xx is the hex for your ASCII character.

For actually replacing characters, you can use java.util.regex.Pattern and the associated Matcher class. If unfamiliar with these, go read their API docs then ask any questions that occur to you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!