Forum Moderators: open

Message Too Old, No Replies

From a String to a Byte Array

How to convert for writing?

         

garann

5:47 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Hi,

I have a String which I need to write directly to the OutputStream object in ASP.NET. I'm trying to create a Microsoft Outlook "VCard", and there are problems when I just create the file from text. Thus, I'd like to build the entire VCard in a StringBuilder object and put that directly into the OutputStream. But I think I need to convert the String or Stringbuilder to a Byte array in order to do that.

Does anyone know how to convert from a String to a Byte array? Or a better way to do this?

Thanks!
g.

korkus2000

6:10 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Heres the ByteConverter class
[msdn.microsoft.com...]

Is that what you want?

garann

10:00 pm on Jan 7, 2004 (gmt 0)

10+ Year Member



korkus, thanks for the link, but I don't think I want to go that route (i.e., Converter classes) until I'm forced to. I could have sworn I'd used a method along the lines of "toByteArray()" in the past. Maybe it's my imagination.

Maybe a better question is whether you can change the filename of the current aspx page from within VbScript?

As it is, I'm produce perfectly workable files, except the file extension doesn't match the content type. I guess I thought that writing data directly to the OutputStream would change it. Could that make a difference?

tomasz

10:26 pm on Jan 7, 2004 (gmt 0)

10+ Year Member



I had similar problem and I created class with c# which i compiled add added to the project. It accepts MemoryStream which is converted to ByteArray so it can be serialized and used in my Web Service. It is kind of mixed code thought. I hope it will help you

using System;
using System.IO;

namespace myUtil
{

public class Utils
{

public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1=theStream.ReadByte())!=-1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
}

}
}

'Now I can use this class and get ArrayByte

Private Function getBinaryFile(ByVal sFile As String) As Byte()
Dim aFile As Byte()

If File.Exists(sFile) Then
Try
Dim s As FileStream = File.OpenRead(sFile)
Dim obj As New myUtil.Utils
aFile = obj.ConvertStreamToByteBuffer(s)
s.Close()
File.Delete(sFile)
obj = Nothing
Return aFile
Catch e As Exception
getBinaryFile = Nothing
End Try
End If
End Function

garann

5:37 pm on Jan 8, 2004 (gmt 0)

10+ Year Member



Thanks for the code, tomasz. However, I'm forced to work with a String, not a file. I'm writing the file on the fly in a StringBuilder object - it never gets saved, just generated and passed to the client.

Writing and serving the file is no problem, but I can't get the file to come across with a file extension that matches its ContentType. Interestingly, it does get the right file extension when I do the same thing in classic ASP. Does anyone know why?