473,883 Members | 1,596 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 15760
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
2498
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
60833
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
79671
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
4
43019
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
7824
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
8924
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
4377
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
5301
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
2022
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
9936
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
9791
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
11137
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
10412
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...
0
9571
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...
0
5797
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...
0
5990
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4215
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3231
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.