0

Is it possible to convert string[] to byte[]? I'm trying to send ICS file but I want to avoid saving it on server and retrieving it back. Here is the code I have so far and it breaks while trying to convert to bytes[]

string schLocation = "Conference Room";
            string schSubject = "Business visit discussion";
            string schDescription = "Schedule description";
            System.DateTime schBeginDate = Convert.ToDateTime("7/13/2014 10:00:00 PM");
            System.DateTime schEndDate = Convert.ToDateTime("7/13/2014 11:00:00 PM");

            //PUTTING THE MEETING DETAILS INTO AN ARRAY OF STRING

            String[] contents = { "BEGIN:VCALENDAR",
                              "PRODID:-//Flo Inc.//FloSoft//EN",
                              "BEGIN:VEVENT",
                              "DTSTART:" + schBeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), 
                              "DTEND:" + schEndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"), 
                              "LOCATION:" + schLocation, 
                         "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + schDescription,
                              "SUMMARY:" + schSubject, "PRIORITY:3", 
                         "END:VEVENT", "END:VCALENDAR" };
            //byte[] data = contents.Select(x => Byte.Parse(x)).ToArray();
            byte[] data = contents.Select(x => Convert.ToByte(x, 16)).ToArray();

            MemoryStream ms = new MemoryStream(data);
            MailMessage message = new MailMessage("[email protected]", "[email protected]");
            message.Subject = schSubject;
            message.Body = "This is test";
            message.IsBodyHtml = false;
            message.Attachments.Add(new Attachment(ms, "meeting.ics"));
            SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]);
            client.Send(message);

I get a following error: Additional non-parsable characters are at the end of the string.

2 Answers 2

3

I would create a single string since your string[] doesn't have any purpose. You can use Encoding.UTF8.GetBytes to get the actual bytes from that string.

In this sample I use a StringBuilder for performance reasons:

StringBuilder sb = new StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("PRODID:-//Flo Inc.//FloSoft//EN");
sb.AppendLine("BEGIN:VEVENT");
sb.AppendLine("DTSTART:" + schBeginDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
sb.AppendLine("DTEND:" + schEndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z"));
sb.AppendLine("LOCATION:" + schLocation);
sb.AppendLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + schDescription);
sb.AppendLine("SUMMARY:" + schSubject, "PRIORITY:3");
sb.AppendLine("END:VEVENT", "END:VCALENDAR");

byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
Sign up to request clarification or add additional context in comments.

Comments

2
string[] abc = new string[]{"hello", "myfriend"};

string fullstring = String.Join(Environment.NewLine, abc);    // Joins all elements in the array together into a single string.
byte[] arrayofbytes = Encoding.Default.GetBytes(fullstring);     // Convert the string to byte array.

1 Comment

Just as a hint: I think the ICS format desires it is multi-line, so for the separator in String.Join use Environment.NewLine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.