473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Convert Byte to String in VB.NET

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 14982
yarbrough40
320 100+
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 100+
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 100+
One problem with using the system.text.encoding 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
by: Daniel | last post by:
in C# fastest way to convert a string into a MemoryStream
2
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...
7
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...
4
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
4
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...
4
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
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...
15
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...
6
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...
1
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,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.