byte array to string

Discussion in 'Ruby' started by Jim Bob, Feb 19, 2007.

  1. Jim Bob

    Jim Bob Guest

    I'm sure the must be code for this already without me having to go
    through the array byte by byte.

    I have an array of bytes, I need to print this as a string?

    Note the array does not contain text.

    Thanks,
    Jim

    --
    Posted via http://www.ruby-forum.com/.
    Jim Bob, Feb 19, 2007
    #1
    1. Advertising

  2. On 2/19/07, Jim Bob <> wrote:
    > I'm sure the must be code for this already without me having to go
    > through the array byte by byte.
    >
    > I have an array of bytes, I need to print this as a string?
    >
    > Note the array does not contain text.


    Your question is ... confusing.

    You have:

    a = [ 65, 32, 66 ]

    When you print it, what do you want to see?

    "65, 32, 66" # use puts a.join(", ")
    "A B" # use puts a.map { |e| e.chr }.join
    "65 32 66" # use puts a.join(" ")
    "0x41, 0x20, 0x42" # use puts a.map { |e| "0x%02x" % e }.join

    So, what are you looking for?

    -austin
    --
    Austin Ziegler * * http://www.halostatue.ca/
    * * http://www.halostatue.ca/feed/
    *
    Austin Ziegler, Feb 19, 2007
    #2
    1. Advertising

  3. Jim Bob

    SonOfLilit Guest

    Strings are the ruby way to store arbitrary stuff in memory.

    You don't necessarily need to access them as strings, they are just
    general dynamically-allocated buffers of memory.

    If you want something similar to C byte arrays, in ruby use strings.

    e.g. for holding pixel data.

    You don't need to PRINT it as a string, you just use String to store it.

    That's why String has methods to get such values and turn them into
    Ruby arrays, that is unpack (and pack, which does the opposite).


    PLUG: http://rubymentor.rubyforge.org/wiki/wiki.pl RubyMentor is
    looking for newbies to Ruby who could use our help
    On 2/19/07, Jim Bob <> wrote:
    > I'm sure the must be code for this already without me having to go
    > through the array byte by byte.
    >
    > I have an array of bytes, I need to print this as a string?
    >
    > Note the array does not contain text.
    >
    > Thanks,
    > Jim
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    SonOfLilit, Feb 19, 2007
    #3
  4. Jim Bob

    Jim Bob Guest

    Thanks for your responses.

    What I was doing was reading in some data from a binary file, this data
    need to be printed in an xml document. I have now used
    Base64.encode64(rawdata) and I am using this to write to the xml file.
    It wasn't working at first, maybe because I opened the file in binary
    mode.

    Thanks,
    Jim.

    --
    Posted via http://www.ruby-forum.com/.
    Jim Bob, Feb 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,209
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    626
    Kirby
    Oct 8, 2004
  3. Replies:
    20
    Views:
    9,699
    licebmi
    Sep 7, 2009
  4. Tom McGlynn
    Replies:
    4
    Views:
    842
    Mark Space
    Apr 19, 2008
  5. Patricia Shanahan
    Replies:
    0
    Views:
    372
    Patricia Shanahan
    Apr 17, 2008
Loading...

Share This Page