473,411 Members | 1,975 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,411 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 3642
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,...
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...
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...
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
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,...
0
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...
0
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,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.