• Post Reply Bookmark Topic Watch Topic
  • New Topic

string custom format  RSS feed

 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method below should receive in some bytes that I need to format for print output in this way:

xx.xx.xxxx

How do I format the variable "message"?



thank you!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I dunno. You have 7 bytes -- which you didn't say anything about beyond the fact that there are seven of them, or maybe fewer. And you've got an output format with 8 x's, which sort of suggests that the x's are supposed to be replaced by something, probably. So could you be a bit more detailed about how the conversion is to take place? If you don't have a specification which somebody gave you, then maybe some examples would help.
 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that. I re-read the specifications for the incoming stream and 6 bytes will be coming in on the datainputstream  - sorry, long day. These bytes will be ASCII codes and will range only from 0-9. The format I'd like to use for display on println will be "xx.xx.xx", e.g., 02.13.12. What custom format can I use to display them as such? Also, is UTF-8 the proper char set to display ASCII?

Thanks again.

 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me correct another mistake: the bytes coming in will be hex, and need to display as ASCII. Sorry, but I had it the wrong way around.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bytes coming in will be hex, and need to display as ASCII.

Can you give an example of that?  Is this what you are asking:  input 0x00 to display as '0' and 0x04 to display as '4' 
Since char can be treated as numbers, then the above conversion can be done by adding '0' to the hex value.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static method String.format() should work for you.  The way to build the formatting string is described here.
 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A stream of six bytes coming in as hex values like so:

30
32
34
39
38
38

Should translate to "024988" in ASCII if I have the UTF-8 setting correct. How can I take those ASCII numbers and format them as "02.49.88"?

Thanks.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: everything in a computer's bytes can be represented in hex.  The hex value of '0' is 0x30 or int 48.  In other words look at the contents of a byte holding '0' and you will see 0x30

See Knute's post about the Formatter class for building a format string.
 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using the String(byte[]) constructor? You may not need to create a String with a constructor in the first place. What you want is to turn those bytes into chars. Are you sure that all the bytes will be strictly < 0x80 (= < 128)? If so, why don't you try this sort of thing?That will only work for characters strictly inside the ASCII coding subrange. You can then put the individual characters into a format String with a %c tag.

You say you have seven bytes, but you declared your array as length six and your format String shows eight xs.

If you have a String, you can create a StringBuilder to encapsulate that String and insert a full stop character at positions 4 and 2 (not 2 and 4). That is probably how I would prefer to do it.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are you sure that all the bytes will be strictly < 0x80 (= < 128)?


Scott A Burch already wrote:These bytes will be ASCII codes and will range only from 0-9.


Given this, then UTF-8 is a red herring. Any encoding which extends ASCII will do, and the default encoding of the system is surely going to extend ASCII no matter what it is. So I would just wrap an InputStreamReader around the socket's input stream (I don't think you need a DataInputStream) and then read six characters from it. Then use String.format to format those characters as required.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!