---
title: "代码段：ASCII to Unicode"
author: "wangdeshui"
date: "2009-04-17T10:44:00Z"
tags: []
slug: "ascii-to-unicode"
draft: false
lastmod: "2009-04-17T10:44:00Z"
id: 404928
url: "https://wangdeshui.iteye.com/blog/404928"
---

```
   1: private string ConvertAsciiToUnicode(string theAsciiString)
```

```
   2:  {
```

```
   3:     // Create two different encodings.
```

```
   4:     Encoding aAsciiEncoding = Encoding.ASCII;
```

```
   5:     Encoding aUnicodeEncoding = Encoding.Unicode;
```

```
   6:  
```

```
   7:     // Convert the string into a byte[].
```

```
   8:     byte[] aAsciiBytes = aAsciiEncoding.GetBytes(theAsciiString);
```

```
   9:  
```

```
  10:     // Perform the conversion from one encoding to the other.
```

```
  11:     byte[] aUnicodeBytes = Encoding.Convert(aAsciiEncoding, aUnicodeEncoding,
```

```
  12:     aAsciiBytes);
```

```
  13:  
```

```
  14:     // Convert the new byte[] into a char[] and then into a string.
```

```
  15:     char[] aUnicodeChars = new
```

```
  16:     char[aUnicodeEncoding.GetCharCount(aUnicodeBytes, 0, aUnicodeBytes.Length)];
```

```
  17:     aUnicodeEncoding.GetChars(aUnicodeBytes, 0, aUnicodeBytes.Length,
```

```
  18:     aUnicodeChars, 0);
```

```
  19:     string aUnicodeString = new string(aUnicodeChars);
```

```
  20:  
```

```
  21:     return aUnicodeString;
```

```
  22: }
```