Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to cast or not to cast

 
John Davis
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a buffer obtained by executing MQMessage.readLine(); This buffer is expected to contain a String, but apparently its first two character positions contain an int. How can I obtain this int? Integer.parseInt will not work and neither with Byte.decode(), and int x = (int) stringAB is illegal.
Everyone thinks I'm weird to ask this question. What am I missing?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is in the first two character positions? If it's a string like "01" you can substring and then use Integer.parseInt(). If it's something binary like 0x00 0x01 then I'm worried about it making the transition from the message to a String and back to a number. You can try taking a substring and getBytes() and working from there.
 
John Davis
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,

Thanks a lot for the fast response! That was a big help.

The first 2 characters contain an int.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic