字符串到字节&反之亦然 [英] String to Byte & Vice Versa
问题描述
大家好,
我使用以下函数将字符串转换为字节,
反之亦然。我完全可耻地从这个
组中偷走了这些功能顺便说一句!无论如何,它们工作得很好但是太慢了。任何人都知道
我如何加速这个功能?我基本上需要将一个字节
转换为字符串,在字符串的每个''section'上执行一个函数,然后
将它重新转换为一个字节。缓慢的部分是转换为
字节,而不是转换后我正在做的部分。
TIA
私有函数ArrayToString(ByVal bytes()As Byte,可选ByVal
格式为String = Nothing)As String
如果bytes.Length = 0则返回String.Empty
Dim sb As New System.Text.StringBuilder(bytes.Length * 4)
For each b As Byte In bytes
sb.Append(b.ToString(格式))
sb.Append("," c)
下一页
sb.Length - = 1
返回sb.ToString()
结束函数
私有函数StringToArray(ByVal s As String,Optional ByVal
style As System.Globalization.NumberStyles = Nothing)As Byte()
如果s.Length = 0则返回新字节(){}
Dim values()As String = s.Split(" ;,c)
Dim bytes(values.Length - 1)As Byte
索引为Integer = 0 To values.Length - 1
bytes(index)= Byte.Parse(EncStr,style)
字节(索引)= Byte.Parse(值(索引),样式)
下一页
返回字节
结束功能
Hi all,
I am using the below functions in order to convert strings to bytes and
vice versa. I totally ans shamefully stole these functions from this
group btw! Anyway, they work great but as sooooo slow. Anyone know
how I can speed this functions up? I basically need to convert a byte
to string, perform a function on each ''section'' of the string, then
reconvert it to a byte. The slow part is the conversion to and from
byte, not the part I am doing in between after conversion.
TIA
Private Function ArrayToString(ByVal bytes() As Byte, Optional ByVal
format As String = Nothing) As String
If bytes.Length = 0 Then Return String.Empty
Dim sb As New System.Text.StringBuilder(bytes.Length * 4)
For Each b As Byte In bytes
sb.Append(b.ToString(format))
sb.Append(","c)
Next
sb.Length -= 1
Return sb.ToString()
End Function
Private Function StringToArray(ByVal s As String, Optional ByVal
style As System.Globalization.NumberStyles = Nothing) As Byte()
If s.Length = 0 Then Return New Byte() {}
Dim values() As String = s.Split(","c)
Dim bytes(values.Length - 1) As Byte
For index As Integer = 0 To values.Length - 1
bytes(index) = Byte.Parse(EncStr, style)
bytes(index) = Byte.Parse(values(index), style)
Next
Return bytes
End Function
推荐答案
" Hugh Janus" < my ************* @ hotmail.comschrieb:
"Hugh Janus" <my*************@hotmail.comschrieb:
我使用以下函数将字符串转换为字节和
反之亦然。我完全可耻地从这个
组中偷走了这些功能顺便说一句!无论如何,它们工作得很好但是太慢了。
I am using the below functions in order to convert strings to bytes and
vice versa. I totally ans shamefully stole these functions from this
group btw! Anyway, they work great but as sooooo slow.
为什么不使用
''System.Text.Encoding.GetString''/''System.Text.Encoding.GetBytes'' ?
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org />
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>
Why not use
''System.Text.Encoding.GetString''/''System.Text.Encoding.GetBytes''?
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
为什么不使用
''System.Text.Encoding.GetString''/''System.Text.Encoding.GetBytes''?
Why not use
''System.Text.Encoding.GetString''/''System.Text.Encoding.GetBytes''?
我没有,所以我认为它是在2.0框架中。我应该
已经提到我正在使用VB 2005.还有其他选择吗?
TIA
I don''t have that so I assume it is in the 2.0 framework. I should
have mentioned that I am using VB 2005. Is there an alternative?
TIA
" Hugh Janus" < my ************* @ hotmail.comschrieb:
"Hugh Janus" <my*************@hotmail.comschrieb:
>为什么不使用
''System.Text.Encoding.GetString''/''System.Text.Encoding.GetBytes''?
>Why not use
''System.Text.Encoding.GetString''/''System.Text.Encoding.GetBytes''?
我没有这个,所以我认为它是在2.0框架中。我应该
提到我正在使用VB 2005.有替代方案吗?
I don''t have that so I assume it is in the 2.0 framework. I should
have mentioned that I am using VB 2005. Is there an alternative?
是的,它从.NET 1.0开始就可用。你必须指定你想要使用的
编码:
\\\
进口系统.Text
....
Dim s As String =" Hello World"
Dim b()As Byte = Encoding.Unicode .GetBytes(s)
s = Encoding.Unicode.GetString(b)
MsgBox(s)
///
< br $> b $ b -
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>
Yes, it''s available since .NET 1.0. You''ll have to specify the encoding you
want to use:
\\\
Imports System.Text
....
Dim s As String = "Hello World"
Dim b() As Byte = Encoding.Unicode.GetBytes(s)
s = Encoding.Unicode.GetString(b)
MsgBox(s)
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
这篇关于字符串到字节&反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!