473,654 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert Byte to String in VB.NET

1 New Member
Hi All!

I had tried to search many forums to get this code.

Yes, just for a single line code. But when everything else failed, I decided to write my own. And it is really working good.

Expand|Select|Wrap|Line Numbers
  1.     Private Function Byte2Str(ByVal gByte() As Byte) As String
  2.         Dim X As Integer
  3.         Dim gTmp As String = ""
  4.         For X = 0 To gByte.Length - 1
  5.             gTmp = gTmp & Chr(gByte(X))
  6.         Next
  7.         Return gTmp
  8.     End Function
Br,
Shadab Ahmad
Mar 18 '11 #1
3 15312
yarbrough40
320 Contributor
this is easier:
Expand|Select|Wrap|Line Numbers
  1. Dim s as String = System.Text.ASCIIEncoding.ASCII.GetString(gByte)
Mar 20 '11 #2
!NoItAll
297 Contributor
It would work faster if you used a stringbuilder instead of string - otherwise it has to recreate the entire string with each concatenation.

Expand|Select|Wrap|Line Numbers
  1. Private Function Byte2Str(ByVal gByte() As Byte) As String
  2.  
  3.         Dim gTmp As New System.Text.StringBuilder
  4.         For X as Int32 = 0 To (gByte.Length - 1)
  5.             gTmp.append(Chr(gByte(X)))
  6.         Next
  7.         Return gTmp.ToString
  8.  
  9. End Function
  10.  
But here is a way that appears to be even better from experts exchange - try this instead

Expand|Select|Wrap|Line Numbers
  1.    Private Function ByteArrayToString(ByVal ByteArray As Byte()) As String
  2.         Return System.Text.Encoding.Unicode.GetString(ByteArray)
  3.     End Function
  4.  
Mar 20 '11 #3
!NoItAll
297 Contributor
One problem with using the system.text.enc oding method is that it will assume you want the data converted to one of the encoding methods. Raw is not one of the methods available and so you can actually wind up with something you may not expect (unless you really do want ASCII, or Unicode, or UTF8).
AFAIK - if you want it raw then you need to use the looping method.
Jun 5 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
716
by: Daniel | last post by:
in C# fastest way to convert a string into a MemoryStream
2
2482
by: Brian | last post by:
I have a string that I received from a web service call and I want to write it to a Stream. I cannot figure out how to access the string as a byte array and write it to the Stream. Any thoughts. I looked at Byte.Parse() but it will only convert a string represntation of a number and convert it to a byte. Thanks, Brian
7
60825
by: Wilfried Mestdagh | last post by:
Hi, how to typecast or convert a byte to a string and the way arount ? I have string from a textbox and wants to transmit it trough a socket. witch accepts a byte. Now I make a copy from the string first, but I'm sure there is some other way. For receiving I have to display the received string into a textbox. same question ? -- rgds, Wilfried
4
79634
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
4
42841
by: John Smith | last post by:
Hello, Suppose I have the following C# code which I want to convert to VB: for (int i = 0; i < nFieldLength; i++) Console.Write((char) sValue); sValue is a byte array. The problem is the typecast which I can't find an equivalent for in VB. I
4
7807
by: Sam | last post by:
Hi, I'm trying to convert a string to a binary format (byte) I've looked at BitConverter class, but I can't figure out how to use it for a Strin. Does anyone know ? Thx
5
8914
by: EOS | last post by:
Hi, Thanks for this great forum and my convertion from Byte into String works beautifully with Encoding.ASCII.GetString() Anyone how to do the reverse, I mean convert a String into Byte? In C\C++, I will cast the CString into LPCTSTR and use simple memcpy. By the way, since I am into C# (from background of C\C++), I felt like
15
4355
by: Steve | last post by:
Hi All, I have a registration code that currently has 4 bytes that are unused. I'd like to store a future date in those 4 bytes. Is there a way to convert a date to a 4 byte string? When I do something like this, string stringDate = DateTime.Parse("1/15/2210").ToBinary().ToString("X");
6
5255
by: Bob Altman | last post by:
Hi all, I'm looking for the fastest way to convert an array of bytes to String. I also need to convert a String back to its original Byte() representation. Convert.ToBase64String and Convert.FromBase64String seem like the closest thing I can find to what I'm looking for baked into the base class library. Can anyone suggest a better way to do this? TIA - Bob
1
2003
by: Slippy27 | last post by:
How do I test a byte string in Python? I want to manually convert (no libraries or functions) a UTF-8 string into UTF-16. My basic solution is to read from the stream some number of UTF-8 bytes, convert them into codepoints, then convert those codepoints into UTF-16 bytes. I want to code this myself, but I don't understand how to test the actual byte sequence. Let's say I use the following code to ensure I have a UTF-8 encoding (from Evan...
0
8372
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
8814
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
8706
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
8475
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
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
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
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.