简体   繁体   中英

A Built-in Function to Convert from String to Byte

I have the following function:

public static byte[] StringToByte(string str)
{
    int length = str.Length;
    byte[] ba = new byte[length];
    for (int i = 0; i < length; i++)
    {           
        ba[i] = (byte)str[i];
    }
    return ba;
}

I wonder whether there is a built-in function for this method?

System.Text.Encoding.GetBytes(string)
System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
byte[] bytes= encoding.GetBytes(stringData);

Does not work. Really - you rappraoch is broken. You ASSUME that the lower byte is the byte you need. Waht you try to achieve? ASCII representation of the string? What codepage?

Check the following page:

http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx

Use one of the classes mentioned there, possibly the ASCIIEncoding class.

The .NET Framework provides the following implementations of the Encoding class to support current Unicode encodings and other encodings:

The .NET Framework provides the following implementations of the Encoding class to support current Unicode encodings and other encodings:

  • ASCIIEncoding encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F. Code page 20127. Also available through the ASCII property.
  • UTF7Encoding encodes Unicode characters using the UTF-7 encoding. This encoding supports all Unicode character values. Code page 65000. Also available through the UTF7 property.
  • UTF8Encoding encodes Unicode characters using the UTF-8 encoding. This encoding supports all Unicode character values. Code page 65001. Also available through the UTF8 property.
  • UnicodeEncoding encodes Unicode characters using the UTF-16 encoding. Both little endian (code page 1200) and big endian (code page 1201) byte orders are supported. Also available through the Unicode property and the BigEndianUnicode property.
  • UTF32Encoding encodes Unicode characters using the UTF-32 encoding. Both little endian (code page 12000) and big endian (code page 12001) byte orders are supported. Also available through the UTF32 property.ASCIIEncoding encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F. Code page 20127. Also available through the ASCII property.
  • UTF7Encoding encodes Unicode characters using the UTF-7 encoding. This encoding supports all Unicode character values. Code page 65000. Also available through the UTF7 property.
  • UTF8Encoding encodes Unicode characters using the UTF-8 encoding. This encoding supports all Unicode character values. Code page 65001. Also available through the UTF8 property.
  • UnicodeEncoding encodes Unicode characters using the UTF-16 encoding. Both little endian (code page 1200) and big endian (code page 1201) byte orders are supported. Also available through the Unicode property and the BigEndianUnicode property.
  • UTF32Encoding encodes Unicode characters using the UTF-32 encoding. Both little endian (code page 12000) and big endian (code page 12001) byte orders are supported. Also available through the UTF32 property.

There is. It is Encoding.GetBytes .

To be exact, and with an example:

public static byte[] StrToByteArray(string str) {
    System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
    return encoding.GetBytes(str);
}

Replace ASCIIEncoding with the encoding you'd like to use.

string s = "Like this";
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] b = enc.GetBytes(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM