473,624 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String to Byte & Vice Versa

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(B yVal 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.Str ingBuilder(byte s.Length * 4)

For Each b As Byte In bytes

sb.Append(b.ToS tring(format))
sb.Append(","c)

Next

sb.Length -= 1

Return sb.ToString()

End Function

Private Function StringToArray(B yVal s As String, Optional ByVal
style As System.Globaliz ation.NumberSty les = Nothing) As Byte()

If s.Length = 0 Then Return New Byte() {}

Dim values() As String = s.Split(","c)

Dim bytes(values.Le ngth - 1) As Byte

For index As Integer = 0 To values.Length - 1

bytes(index) = Byte.Parse(EncS tr, style)
bytes(index) = Byte.Parse(valu es(index), style)

Next

Return bytes

End Function

Nov 30 '06 #1
16 2142
"Hugh Janus" <my************ *@hotmail.comsc hrieb:
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.
Why not use
'System.Text.En coding.GetStrin g'/'System.Text.En coding.GetBytes '?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 30 '06 #2
Why not use
'System.Text.En coding.GetStrin g'/'System.Text.En coding.GetBytes '?
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

Nov 30 '06 #3
"Hugh Janus" <my************ *@hotmail.comsc hrieb:
>Why not use
'System.Text.E ncoding.GetStri ng'/'System.Text.En coding.GetBytes '?

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?
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.Unicod e.GetBytes(s)
s = Encoding.Unicod e.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/>

Nov 30 '06 #4
>I should have mentioned that I am using VB 2005. Is there an alternative?

Oops. I am using 2003, not 2005.

Nov 30 '06 #5
I am using 2003 too
instead of using class names u just have to use instances (because its
a shared function)

This will compile

I didn't run it ...any way try it

Dim instance As System.Text.Enc oding
Dim bytes As Byte()
Dim returnValue As String

returnValue = instance.GetStr ing(bytes)

for more info check this link

http://msdn2.microsoft.com/en-us/library/744y86tc.aspx

Nov 30 '06 #6
"roader" <ro******@gmail .comschrieb:
>I am using 2003 too
instead of using class names u just have to use instances (because its
a shared function)

This will compile

I didn't run it ...any way try it

Dim instance As System.Text.Enc oding
Dim bytes As Byte()
Dim returnValue As String

returnValue = instance.GetStr ing(bytes)
.... but it will throw a 'NullReferenceE xception' at runtime :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 30 '06 #7
This code will work .....
I am sorry i was little lazy...

Dim instance As System.Text.Enc oding = System.Text.Enc oding.UTF8

Dim returnValue As String
Dim b As Byte() = {&H34, &H56, &HAA, &H55, &HFF}
returnValue = ""

returnValue = instance.GetStr ing(b)
MsgBox(returnVa lue)
Best of luck

Nov 30 '06 #8
This is done is both vb2005 and vb2003.
Be sure to add the 'System.Text' Reference to your project

Imports System.Text 'Import the text namespace

Public sub ConvertStringTo Byte(StringToCo nvert as string)
Dim b() as byte 'A byte array
b = Encoding.GetByt es(StringToConv ert)
end sub

'Convert Byte To String

Public Sub CovertByteToStr ing(BytesToConv ert() as byte)
Dim s as string 'An empty string
s = Encoding.Text.G etString(BytesT oConvert)
end sub

Hope this awnsers your question. Good Luck!


--
--
Thiele Enterprises - The Power Is In Your Hands Now!
--
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.atwrote in message
news:ex******** ******@TK2MSFTN GP02.phx.gbl...
"roader" <ro******@gmail .comschrieb:
>I am using 2003 too
instead of using class names u just have to use instances (because its
a shared function)

This will compile

I didn't run it ...any way try it

Dim instance As System.Text.Enc oding
Dim bytes As Byte()
Dim returnValue As String

returnValue = instance.GetStr ing(bytes)
.... but it will throw a 'NullReferenceE xception' at runtime :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 30 '06 #9


"roader" <ro******@gmail .comwrote in message
news:11******** **************@ l39g2000cwd.goo glegroups.com.. .
>I am using 2003 too
instead of using class names u just have to use instances (because its
a shared function)
Huh? You have to use instances "because it’s a shared function"??? A
shared method means you don't use an instance, you use a class name :)
Although, in VB.Net you can use instance variables to call shared methods
(which I think shouldn't be allowed, but it is)..
>
This will compile

I didn't run it ...any way try it

Dim instance As System.Text.Enc oding
Dim bytes As Byte()
Dim returnValue As String

returnValue = instance.GetStr ing(bytes)
Why not just System.Text.Enc oding.UTF8.GetS tring(bytes)?

HTH,
Mythran
Nov 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8454
by: mailar | last post by:
HI, Can anyone tell me how is a multi_byte to single byte and vice versa conversion done in DB2. It would be great even if someone can tell me how Oracle does it? Oracle already has functions called to_single_byte and to_multi_byte which convert a given multi byte character to single byte and vice versa respectively. thanks in advance mailar
7
26773
by: Matthias S. | last post by:
Hi, I had a look at the vast information on encryption in the MSDN and got pretty confused. All I want to do is to encrypt a string into an encrypted string and later decrypt that (encrypted) string again to a human readable form. Can't be that difficult :). Could you send me please into the right direction. Thanks in advance. --
7
7588
by: Eric | last post by:
Hi All, I need to XOR two same-length Strings against each other. I'm assuming that, in order to do so, I'll need to convert each String to a BitArray. Thus, my question is this: is there an easy way to convert a String to a BitArray (and back again)? I explained the ultimate goal (XORing two Strings) so that, if anyone has a better idea of how to go about this they may (hopefully) bring that up...?
4
3762
by: Khalique | last post by:
I have built a web service whose purpose is to copy files from a secure place to client machine and vice versa. The problem I am having is perhaps related to permissions and access rights. For testing purposes, the secure place is setup on the client machine. The client (window app) calls the web service (on a different machine) and connects successfully to the web service. However, when client calls a method that copies the file from...
1
2508
by: Eugene Anthony | last post by:
Private Function BStr2UStr(BStr) 'Byte string to Unicode string conversion Dim lngLoop BStr2UStr = "" For lngLoop = 1 to LenB(BStr) BStr2UStr = BStr2UStr & Chr(AscB(MidB(BStr,lngLoop,1))) Next End Function Private Function UStr2Bstr(UStr)
2
23976
yabansu
by: yabansu | last post by:
Hi all, This is my first message! Using C++ standard libraries, I want to convert a string to a byte array and a byte array to the string. How can I do that? I did it in C# .NET as the following: string msg = "Hello!"; byte buffer = new byte;
6
39270
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now, I want to do the same thing in pure(unmanaged) C++. I searched the Internet but could not find any satisfactory solutions. I am really in need of help! Is there anyone to explain how I can implement these functions by not using .NET library? ...
4
3855
by: arunfr | last post by:
Hi , I have tried all the methods available to convert byte () to string and vice versa in VBSCRIPT. Function ByteArray2Text(varByteArray) Dim rs Const adLongVarChar = 201 Set rs = CreateObject("ADODB.Recordset") rs.Fields.Append "temp", adLongVarChar, LenB(varByteArray) rs.Open rs.AddNew
6
2252
by: Ole | last post by:
I have a class that I wish to convert into a byte array for sending over a serial line and vice versa - how do I do that? Thanks and regards Ole
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.