• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Give a byte, get a char,,,

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on a parser for a binary file format. I'll be getting ints from the Java reader that represent the values of each byte in the file being parsed. Many of these bytes represent ASCII text. I'm looking for the best way to:

[1] Convert a single int representing the value of a byte read from the binary file into a char or String that represents the corresponding ASCII value.

[2] Convert a group of ints representing the value of a series of bytes read from the binary file into a single String.

I think I can do #1 using the toString() method of the Byte class. Is this the best way? How would I accomplish number 2?

Thanks for the help.

Landon
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best way to do both of them is to create a byte array from the input (perhaps containing only one element) and then to use the new String(byte[]) constructor. Both of the toString() methods in Byte (the static and instance methods) convert a byte to a String representing the binary value of that byte. That isn't what you want to do.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic