2014-11-08 50 views
-1

难以将字符串“23,200,237,15”转换为与.net等效的字节。尝试了几种不同的方式并试图坚持分裂。 Bitconverter似乎太低效了。将字节串转换为字节数组

+3

请发表您试过的内容,以及出错的地方。你有错误,或者结果不同于预期?请具体说明。 – mihai 2014-11-08 16:38:48

+0

没有错误,我不能完全弄清楚逻辑/关键字来完成它。 – 2014-11-10 18:08:14

回答

0

有很多SOOOO方式去这...这里有一个基本的方法:

Dim strBytes As String = "23,200,237,15" 

    Dim lstBytes As New List(Of Byte) 
    For Each strByte As String In strBytes.Split(",".ToCharArray) 
     Dim b As Byte 
     If Byte.TryParse(strByte, b) Then 
      lstBytes.Add(b) 
     Else 
      MessageBox.Show("Invalid Byte: " & strByte) 
     End If 
    Next 

    ' use 'lstBytes' directly, or convert it to an array: 
    Dim arrBytes() As Byte = lstBytes.ToArray 

*你可以用这些类型的使用LINQ的东西真正看中的,并用一个班轮最终是这一切都完成了。

+0

真棒,我认为tryparse是我失踪。将尝试解析您的示例;) – 2014-11-10 18:24:25

0

你可以使用这个。

const string input =“stack overflow is good”;

// Invoke GetBytes method. 
// ... You can store this array as a field! 
byte[] array = Encoding.ASCII.GetBytes(input); 
+0

您正在接受一个字符串并将其转换,我正在寻找要转换为字节的确切数字。例如,将字符串“23”转换为字节23。 – 2014-11-10 18:10:01

相关问题