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

Converting byte[] to string

Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte[] and I'm trying to convert the result
to String but the converted string is altered, so if I'm generating a new
wave file from that string, the continment is altered from it's original
state. Below is a code snipped I'm using:

// Here I read the wave file and I convert the result to a string
byte[] b = new byte[35000];
FileStream fs = File.OpenRead("test.wav");
int size = fs.Read(b, 0, b.Length);
string dataSample = Encoding.ASCII.GetString(b, 0, size);
// Here I generate a second wave file from the result but the generated file
contains noise and disturbances
FileStream f = File.Create("test2.wav");
f.Write(Encoding.ASCII.GetBytes(dataSample), 0, size);

I also tryied some other encoding types like UTF7, UTF8, Default and
ISO-8859-1 but the result is the same.
I'm using a function from an assembly which needs a String parameter as
input that should be a wave buffer.
Can somebody help me? What am I doing wrong? Is there another way to read a
binary file in a String object?
Nov 16 '05 #1
8 3637
Marius Cabas <ma**********@hotmail.com> wrote:
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte[] and I'm trying to convert the result
to String


That's a very bad idea. Strings are for text data. Wave files are
binary data. Just keep it in byte array format.

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

You can use Convert.ToBase64String & Convert.FromBase64String to correctly
encode and decode binary data in a string.

However, I suspect this is not really what you want to do. It looks like the
problem may be in the semantics of the method in the assembly you are trying
to call. Does this really expect a string? And if so, perhaps there is some
documentation somewhere which specifies what format this string should be. As
Jon said in the previous post, strings are not used to store binary data.

If there is still a problem, perhaps you can post some details of the method
& assembly you are trying to call.

Cheers,
Chris.

"Marius Cabas" wrote:
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte[] and I'm trying to convert the result
to String but the converted string is altered, so if I'm generating a new
wave file from that string, the continment is altered from it's original
state. Below is a code snipped I'm using:

// Here I read the wave file and I convert the result to a string
byte[] b = new byte[35000];
FileStream fs = File.OpenRead("test.wav");
int size = fs.Read(b, 0, b.Length);
string dataSample = Encoding.ASCII.GetString(b, 0, size);
// Here I generate a second wave file from the result but the generated file
contains noise and disturbances
FileStream f = File.Create("test2.wav");
f.Write(Encoding.ASCII.GetBytes(dataSample), 0, size);

I also tryied some other encoding types like UTF7, UTF8, Default and
ISO-8859-1 but the result is the same.
I'm using a function from an assembly which needs a String parameter as
input that should be a wave buffer.
Can somebody help me? What am I doing wrong? Is there another way to read a
binary file in a String object?

Nov 16 '05 #3
Hi,

Why are you converting a wave file that is a BINARY file to text ?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marius Cabas" <ma**********@hotmail.com> wrote in message
news:uR**************@TK2MSFTNGP14.phx.gbl...
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte[] and I'm trying to convert the result
to String but the converted string is altered, so if I'm generating a new
wave file from that string, the continment is altered from it's original
state. Below is a code snipped I'm using:

// Here I read the wave file and I convert the result to a string
byte[] b = new byte[35000];
FileStream fs = File.OpenRead("test.wav");
int size = fs.Read(b, 0, b.Length);
string dataSample = Encoding.ASCII.GetString(b, 0, size);
// Here I generate a second wave file from the result but the generated file contains noise and disturbances
FileStream f = File.Create("test2.wav");
f.Write(Encoding.ASCII.GetBytes(dataSample), 0, size);

I also tryied some other encoding types like UTF7, UTF8, Default and
ISO-8859-1 but the result is the same.
I'm using a function from an assembly which needs a String parameter as
input that should be a wave buffer.
Can somebody help me? What am I doing wrong? Is there another way to read a binary file in a String object?

Nov 16 '05 #4
because I have to pass the binary stream to an SSL socket function that
takes a String object as a parameter.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:#j**************@TK2MSFTNGP14.phx.gbl...
Hi,

Why are you converting a wave file that is a BINARY file to text ?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marius Cabas" <ma**********@hotmail.com> wrote in message
news:uR**************@TK2MSFTNGP14.phx.gbl...
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte[] and I'm trying to convert the result to String but the converted string is altered, so if I'm generating a new wave file from that string, the continment is altered from it's original
state. Below is a code snipped I'm using:

// Here I read the wave file and I convert the result to a string
byte[] b = new byte[35000];
FileStream fs = File.OpenRead("test.wav");
int size = fs.Read(b, 0, b.Length);
string dataSample = Encoding.ASCII.GetString(b, 0, size);
// Here I generate a second wave file from the result but the generated file
contains noise and disturbances
FileStream f = File.Create("test2.wav");
f.Write(Encoding.ASCII.GetBytes(dataSample), 0, size);

I also tryied some other encoding types like UTF7, UTF8, Default and
ISO-8859-1 but the result is the same.
I'm using a function from an assembly which needs a String parameter as
input that should be a wave buffer.
Can somebody help me? What am I doing wrong? Is there another way to

read a
binary file in a String object?


Nov 16 '05 #5
Yeah, I know this but I have to do it because I'm using an assembly wrote by
a third party. This assembly contains an SSL socket class that takes a
String object as a parameter. This String object keeps the data to send over
TCP/IP via SSL. I have no choice :(

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Marius Cabas <ma**********@hotmail.com> wrote:
Hi, I'm a beginner so don't shoot ;)
I'm reading a wave file into a byte[] and I'm trying to convert the result to String


That's a very bad idea. Strings are for text data. Wave files are
binary data. Just keep it in byte array format.

--
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
Marius Cabas <ma**********@hotmail.com> wrote:
Yeah, I know this but I have to do it because I'm using an assembly wrote by
a third party. This assembly contains an SSL socket class that takes a
String object as a parameter. This String object keeps the data to send over
TCP/IP via SSL. I have no choice :(


Hmm... I would contact the third party and check this. SSL is designed
for streams really - there's no justifiable reason why you *should*
have to specify everything in terms of strings. It's just asking for
trouble.

Are you able to specify the encoding the SSL code will use?

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message

Are you able to specify the encoding the SSL code will use?


No, I have no control. I can only connect to a remote host using a por
number and I can set the certificates. Afterwards, I can read and write data
from/to the socket.
Nov 16 '05 #8
Marius Cabas <ma**********@hotmail.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
Are you able to specify the encoding the SSL code will use?


No, I have no control. I can only connect to a remote host using a por
number and I can set the certificates. Afterwards, I can read and write data
from/to the socket.


And you can only read/write data from/to the socket in string form?
What a terrible interface.

Basically, you won't be able to transfer binary data correctly unless
you can use something like Base64 encoding at both ends. If you don't
have control over the other end, you're stuffed.

Is there any way you can ditch this library and use a different one? It
sounds awful...

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

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

Similar topics

7
by: i_vincent | last post by:
Hi all, Newbie Python programmer here, so please be patient. I have spent all day googling for an answer to my problem, but everything I try fails to work (or works from the Interpreter with a...
2
by: tgif | last post by:
Can someone explain the format for converting a .NET datatype (such as string or int) to a database type (such as tinyint, datetime) for use in a SqlParameter.
4
by: Prabhu | last post by:
Hi, We are having problem in converting a byte array to string, The byte array has char(174), char(175), char(240), char(242) and char(247) as delimiters for the message. when we use...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
1
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...
1
by: Kevin S Gallagher | last post by:
I found this code (pretty sure it was from a MVP) for converting a string variable to a form object which works fine within a form. Take the code and place it into a code module and it fails on the...
0
by: rrp83 | last post by:
Hi All Can anyone fwd me the code for converting a string to bytes similar to the GetBytes function in C# and viceversa?? Regards, RRP83
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...
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,...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.