473,394 Members | 1,960 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,394 software developers and data experts.

convert byte[] to string and way around

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
http://www.mestdagh.biz
Nov 16 '05 #1
7 60802
> 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 ?

Check out this class: "BitConverter"


--
Je**@programeri.org
http://www.programeri.org
http://forum.programeri.org
Nov 16 '05 #2
Hi,

string s = System.Text.Encoding.UTF8.GetString(bytes);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);

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 ?

Nov 16 '05 #3
Wilfried,

Normally is this the class for what you ask.
http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
Nov 16 '05 #4
Hi,

thank it is workinbut followig proble ifr receiving data:

private void DoDataAvailable(byte[] buffer, int offset, int count)

how to convert this to string ? offset is offset begin of string, and count
is amountof data.
Nov 16 '05 #5
Wilfried Mestdagh <Wi**************@discussions.microsoft.com> wrote:
thank it is workinbut followig proble ifr receiving data:

private void DoDataAvailable(byte[] buffer, int offset, int count)

how to convert this to string ? offset is offset begin of string, and count
is amountof data.


Denny already showed you the code - use Encoding.GetString.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hi Jon,

Sorr I was not clear in this question. I have a pointer to a buffer with
offset in ti where data start, and count of data. To convert to string now I
need to make a double copy whitch should not be. I show because it will
explain better:

private void DoDataAvailable(byte[] buffer, int offset, int count)
{
count -= 2; // remove \r\n
byte[] tmp = new byte[count];
while (--count >= 0)
tmp[count] = buffer[offset + count];
string rx = Encoding.UTF8.GetString(tmp);

I wanted to avoid the extra copy of the data in tmp here.

thx, Wilfried
Nov 16 '05 #7
Wilfried Mestdagh <Wi**************@discussions.microsoft.com> wrote:
Sorr I was not clear in this question. I have a pointer to a buffer with
offset in ti where data start, and count of data. To convert to string now I
need to make a double copy whitch should not be. I show because it will
explain better:

private void DoDataAvailable(byte[] buffer, int offset, int count)
{
count -= 2; // remove \r\n
byte[] tmp = new byte[count];
while (--count >= 0)
tmp[count] = buffer[offset + count];
string rx = Encoding.UTF8.GetString(tmp);

I wanted to avoid the extra copy of the data in tmp here.


Use the overload of Encoding.GetString which takes a start index and a
count.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8

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

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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.