C#
  Home arrow C# arrow Strings and Characters, Part 2
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Strings and Characters, Part 2
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2004-08-03

    Table of Contents:
  • Strings and Characters, Part 2
  • 2.14 Passing a String to a Method that Accepts Only a Byte[ ]
  • 2.15 Converting Strings to Their Equivalent Value Type
  • 2.16 Formatting Data in Strings Problem
  • 2.17 Creating a Delimited String
  • 2.18 Extracting Items from a Delimited String
  • 2.19 Setting the Maximum Number of Characters a String Can Contain
  • 2.20 Iterating Over Each Character in a String
  • 2.21 Improving String Comparison Performance
  • 2.22 Improving StringBuilder Performance
  • 2.23 Pruning Characters from the Head and/or Tail of a String

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Strings and Characters, Part 2


    (Page 1 of 11 )

    This half of the chapter on Strings and Characters starts with how to return a byte[] consisting of characters instead of a string, and covers other topics such as converting string types to their equvalent value type and creating a delimited string. (From C# Cookbook by Stephen Teilhet and Jay Hilyard, O'Reilly Media, ISBN: 0596003390, 2004.)

    c sharp cookbookPart 1 of this article is available at this link.

    2.13 Converting a String Returned as a Byte[ ] Back into a String

    Problem

    Many methods in the FCL return a byte[] consisting of characters instead of a string. Some of these methods include:




    System.Net.Sockets.Socket.Receive System.Net.Sockets.Socket.ReceiveFrom System.Net.Sockets.Socket.BeginReceive System.Net.Sockets.Socket.BeginReceiveFrom System.Net.Sockets.NetworkStream.Read System.Net.Sockets.NetworkStream.BeginRead System.IO.BinaryReader.Read System.IO.BinaryReader.ReadBytes System.IO.FileStream.Read System.IO.FileStream.BeginRead System.IO.MemoryStream // Constructor System.IO.MemoryStream.Read System.IO.MemoryStream.BeginRead System.Security.Cryptography.CryptoStream.Read System.Security.Cryptography.CryptoStream.BeginRead System.Diagnostics.EventLogEntry.Data

    In many cases, this byte[] might contain ASCII or Unicode encoded characters. You need a way to recombine this byte[] to obtain the original string.

    Solution

    To convert a byte array of ASCII values to a complete string, use the following method:

    using System;
    using System.Text;

    public static string FromASCIIByteArray(byte[] characters)

    {

    ASCIIEncoding encoding = new ASCIIEncoding();

    string constructedString = encoding.GetString(characters);

    return (constructedString);
    }

    To convert a byte array of Unicode values (UTF-16 encoded) to a complete string, use the following method:

    public static string FromUnicodeByteArray(byte[] characters)

    {

    UnicodeEncoding encoding = new UnicodeEncoding();

    string constructedString = encoding.GetString(characters);

    return (constructedString);
    }

    Discussion

    The GetString method of the ASCIIEncoding class converts 7-bit ASCII characters contained in a byte array to a string. Any value larger than 127 is converted to the ? character. The ASCIIEncoding class can be found in the System.Text namespace. The GetString method is overloaded to accept additional arguments as well. The overloaded versions of the method convert all or part of a string to ASCII and then store the result in a specified range inside a byte array.

    The GetString method returns a string containing the converted byte array of ASCII characters.

    The GetString method of the UnicodeEncoding class converts Unicode characters into 16-bit Unicode values. The UnicodeEncoding class can be found in the System.Text namespace. The GetString method returns a string containing the converted byte array of Unicode characters.

    See Also

    See the “ASCIIEncoding Class” and “UnicodeEncoding Class” topics in the MSDN documentation.

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.

    More C# Articles
    More By O'Reilly Media


       · The encoder no longer accepts byte arrays only ubyte arrays so this information is...
     

    C# ARTICLES

    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT