472,787 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,787 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 3578
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
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.