This Question is Possibly Answered

1 "correct" answer available (5 pts) 15 "helpful" answers available (3 pts)
6 Replies Last post: Sep 30, 2008 9:04 AM by cbarber

How to convert String To Byte[]?

Sep 29, 2008 11:27 PM

Click to view lavkesh_rathi's profile Level 2 lavkesh_rathi 35 posts since
Aug 17, 2008
How to convert String To Byte[]?
Click to view friedger's profile MVP friedger 108 posts since
Jan 13, 2008
1. Re: How to convert String To Byte[]? Sep 30, 2008 1:26 AM
Probably you mean Array-of byte?

Use

{read-bytes-from {string-url "my string"}}

See also http://developers.curl.com/userdocs/docs/en/api-ref/read-bytes-from.html
Click to view tiju's profile Level 6 tiju 102 posts since
Oct 17, 2007
2. Re: How to convert String To Byte[]? Sep 30, 2008 1:52 AM
in response to: friedger

Hi Friedger, what about converting a double to string than appending the double to a blank string? Any better way for that too?
Click to view friedger's profile MVP friedger 108 posts since
Jan 13, 2008
3. Re: How to convert String To Byte[]? Sep 30, 2008 2:26 AM
in response to: tiju

Just use the default factory, i.e. {String 37.7}

Friedger

Click to view tiju's profile Level 6 tiju 102 posts since
Oct 17, 2007
4. Re: How to convert String To Byte[]? Sep 30, 2008 2:42 AM
in response to: friedger
Oops! Thanks Friedger
Click to view mgordon's profile Curl mgordon 47 posts since
Oct 17, 2007
5. Re: How to convert String To Byte[]? Sep 30, 2008 7:10 AM
in response to: friedger
Also, for converting numbers and other data types to strings, look up the format macro, and NumberFormatter for more advanced formatting of numbers. There's also a DateTimeFormatter for dates and times.
Click to view cbarber's profile Curl cbarber 115 posts since
Sep 27, 2007
6. Re: How to convert String To Byte[]? Sep 30, 2008 9:04 AM
To convert a String (or, more generally, a StringInterface) to a ByteVec (aka {FastArray-of byte}) or ByteArray (aka {Array-of byte}) you will probably want to specify the character encoding you want to use. You can then use a TranscodingTextOutputStream to encode the characters and write them into a ByteArray through a ByteArrayOutputStream:

{define-proc {string-to-bytes string:StringInterface}:ByteArray
  def bytes = {ByteArray}
  {with-open-streams
     out = {TranscodingTextOutputStream
                 {ByteArrayOutputStream bytes},
                 CharEncoding.utf8
           }
  do
     {out.write-one-string string}
  }
  {return bytes}
}


If you know the size of the byte vec you need you can just use the built-in 'encode-characters' function to do this. If you don't mind the vector having wasted space in it you can write:

def bytes = {ByteVec max-size = string.size * char-encoding.transcoding-max-expansion-factor}
{encode-characters string, bytes, char-encoding}


Friedger's trick will work but is a little slow and does not specify the character encoding -- I think it will use UTF8, but I am not sure.

(I haven't tested any of this code, but I think it is correct.)