C#
  Home arrow C# arrow Page 2 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: 5 stars5 stars5 stars5 stars5 stars / 7
    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 - 2.14 Passing a String to a Method that Accepts Only a Byte[ ]


    (Page 2 of 11 )

    Problem

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

    System.Net.Sockets.Socket.Send System.Net.Sockets.Socket.SendTo System.Net.Sockets.Socket.BeginSend System.Net.Sockets.Socket.BeginSendTo System.Net.Sockets.NetworkStream.Write System.Net.Sockets.NetworkStream.BeginWrite System.IO.BinaryWriter.Write System.IO.FileStream.Write System.IO.FileStream.BeginWrite System.IO.MemoryStream.Write System.IO.MemoryStream.BeginWrite System.Security.Cryptography.CryptoStream.Write System.Security.Cryptography.CryptoStream.BeginWrite System.Diagnostics.EventLog.WriteEntry

    In many cases, you might have a string that you need to pass into one of these methods or some other method that only accepts a byte[]. You need a way to break up this string into a byte[].

    Solution

    To convert a string to a byte array of ASCII values, use the GetBytes method on an instance of the ASCIIEncoding class:

    using System;
    using System.Text;

    public static byte[] ToASCIIByteArray(string characters)

    {

    ASCIIEncoding encoding = new ASCIIEncoding();

    int numberOfChars = encoding.GetByteCount(characters);

    byte[] retArray = new byte[numberOfChars];

    retArray = encoding.GetBytes(characters);

    return (retArray);
    }

    To convert a string to a byte array of Unicode values, use the UnicodeEncoding class:

    public static byte[] ToUnicodeByteArray(string characters)

    {

    UnicodeEncoding encoding = new UnicodeEncoding();

    int numberOfChars = encoding.GetByteCount(characters);

    byte[] retArray = new byte[numberOfChars];

    retArray = encoding.GetBytes(characters);

    return (retArray);
    }

    Discussion

    The GetBytes method of the ASCIIEncoding class converts ASCII characters—con-tained in either a char array or a string—into a byte array of 7-bit ASCII values. Any value larger than 127 is converted to the ? character. The ASCIIEncoding class can be found in the System.Text namespace. The GetBytes 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, which is returned to the caller.

    The GetBytes 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 GetBytes method returns a byte array, each element of which contains the Unicode value of a single character of the string.

    A single Unicode character in the source string or in the source char array corresponds to two elements of the byte array. For example, the following byte array contains the ASCII value of the letter 'S':

    byte[] sourceArray = {83};

    However, for a byte array to contain a Unicode representation (UTF-16 encoded) of the letter 'S', it must contain two elements. For example:

    byte[] sourceArray = {83, 0};

    The Intel architecture uses a little-endian encoding, which means that the first element is the least-significant byte and the second element is the most-significant byte. Other architectures may use big-endian encoding, which is the opposite of littleendian encoding. The UnicodeEncoding class supports both big-endian and littleendian encodings. Using the UnicodeEncoding instance constructor, you can construct an instance that uses either big-endian or little-endian ordering. In addition, you have the option to indicate whether a byte order mark preamble should be generated so that readers of the file will know which endianness is in use.

    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

    - 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#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





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