![](/img/trans.png)
[英]C# Is there a built-in function to convert a formatted String back to a Number?
[英]A Built-in Function to Convert from String to Byte
我有以下功能:
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;
}
我想知道這個方法是否有內置函數?
System.Text.Encoding.GetBytes(string)
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
byte[] bytes= encoding.GetBytes(stringData);
不行。 真的 - 你的rappraoch壞了。 您假設低位字節是您需要的字節。 你試圖實現嗎? 字符串的ASCII表示? 什么代碼頁?
請查看以下頁面:
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx
使用其中提到的類之一,可能是ASCIIEncoding類。
.NET Framework提供了Encoding類的以下實現,以支持當前的Unicode編碼和其他編碼:
.NET Framework提供了Encoding類的以下實現,以支持當前的Unicode編碼和其他編碼:
有。 它是Encoding.GetBytes
。
確切地說,並舉例說明:
public static byte[] StrToByteArray(string str) {
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
將ASCIIEncoding替換為您要使用的編碼。
string s = "Like this";
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] b = enc.GetBytes(s);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.