Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
byte array to string
Jim
2/19/2007 6:10:00 PM
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-...
.
3 Answers
Austin Ziegler
2/19/2007 6:37:00 PM
0
On 2/19/07, Jim Bob <james_b@anytimenow.com> 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 * halostatue@gmail.com *
http://www.halo...
* austin@halostatue.ca *
http://www.halo...
feed/
* austin@zieglers.ca
SonOfLilit
2/19/2007 6:40:00 PM
0
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/wi...
RubyMentor is
looking for newbies to Ruby who could use our help
On 2/19/07, Jim Bob <james_b@anytimenow.com> 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-...
.
>
>
Jim
2/19/2007 6:52:00 PM
0
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-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
byte array to string
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password