Problem converting byte[] to string

S

scott

hi all, hope some one can help me, this prob is driving me mad.



im using sockets to communicate between a client and a server. I don't'
have control over the client and how it sends the data so i have had to try
and work out how it is doing it. From what i can see it is sending the data
in ASCII because if i try to use Unicode it just stops working, where's with
ASCII i can get response from it and send and receive data.



The problem.



The client sends to me a load of data, most of it is ok however some of the
data contains special text like §. When i try to decode one of these or
anything else like it the decoding just goes wrong and it reads it as a _.
I know that it must be possible because other people have been able to
communicate with the client and use these special characters but i can't
seem to.



Does any one have any ideas ?



Thx scott.
 
N

Nicholas Paldino [.NET/C# MVP]

Scott,

Why not just find out what encoding those other clients are using? The
server should be using some well-defined encoding scheme (ASCII, UTF8, or
something of that nature). When you find out what it is, just find the
corresponding .NET encoding class (in the System.Text namespace, most of
them are there), and run your bytes through that.

If it is not a well-defined encoding, then I would take the rules that
make up how the bytes are sent/transformed and derive a class from Encoding
that encapsulates these rules.

Hope this helps.
 
S

scott

Thx for the quick reply

I did acutlay try all the ones that exist in System.Text namespace and the
only one that worked was the ASCII one. it just did not do them special
charectors. I was just wondering if any one new a reason as to why this was
so that i could correct the problem. If no one knows to a reason as to why
its not doing it ill keep hunting around for an answer.

Nicholas Paldino said:
Scott,

Why not just find out what encoding those other clients are using? The
server should be using some well-defined encoding scheme (ASCII, UTF8, or
something of that nature). When you find out what it is, just find the
corresponding .NET encoding class (in the System.Text namespace, most of
them are there), and run your bytes through that.

If it is not a well-defined encoding, then I would take the rules that
make up how the bytes are sent/transformed and derive a class from Encoding
that encapsulates these rules.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


scott said:
hi all, hope some one can help me, this prob is driving me mad.



im using sockets to communicate between a client and a server. I don't'
have control over the client and how it sends the data so i have had to
try
and work out how it is doing it. From what i can see it is sending the
data
in ASCII because if i try to use Unicode it just stops working, where's
with
ASCII i can get response from it and send and receive data.



The problem.



The client sends to me a load of data, most of it is ok however some of
the
data contains special text like §. When i try to decode one of these or
anything else like it the decoding just goes wrong and it reads it as a _.
I know that it must be possible because other people have been able to
communicate with the client and use these special characters but i can't
seem to.



Does any one have any ideas ?



Thx scott.
 
N

Nicholas Paldino [.NET/C# MVP]

Scott,

If it doesn't work for the ASCII one, then the encoding isn't ASCII.
Rather, it is a special implementation based on the protocol. It's not that
the encoding is wrong, it's that the protocol is doing something
non-standard.

Do you have more information as to what the protocol is expecting/doing?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

scott said:
Thx for the quick reply

I did acutlay try all the ones that exist in System.Text namespace and the
only one that worked was the ASCII one. it just did not do them special
charectors. I was just wondering if any one new a reason as to why this
was
so that i could correct the problem. If no one knows to a reason as to
why
its not doing it ill keep hunting around for an answer.

in
message news:%[email protected]...
Scott,

Why not just find out what encoding those other clients are using? The
server should be using some well-defined encoding scheme (ASCII, UTF8, or
something of that nature). When you find out what it is, just find the
corresponding .NET encoding class (in the System.Text namespace, most of
them are there), and run your bytes through that.

If it is not a well-defined encoding, then I would take the rules
that
make up how the bytes are sent/transformed and derive a class from Encoding
that encapsulates these rules.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


scott said:
hi all, hope some one can help me, this prob is driving me mad.



im using sockets to communicate between a client and a server. I
don't'
have control over the client and how it sends the data so i have had to
try
and work out how it is doing it. From what i can see it is sending the
data
in ASCII because if i try to use Unicode it just stops working, where's
with
ASCII i can get response from it and send and receive data.



The problem.



The client sends to me a load of data, most of it is ok however some of
the
data contains special text like §. When i try to decode one of these
or
anything else like it the decoding just goes wrong and it reads it as a _.
I know that it must be possible because other people have been able to
communicate with the client and use these special characters but i
can't
seem to.



Does any one have any ideas ?



Thx scott.
 
S

scott

Thx for your help in this. Iv managed to solve the prob.

None of the decoders worked in System.Text namespace.

for some reason i decied to try a simple cast of each byte to a char and it
worked.
The following is what i did.
char[] c= new char[Data.Length];

for (int i = 0; i < Data.Length; i++)

{

c = (char)Data;

}



Nicholas Paldino said:
Scott,

If it doesn't work for the ASCII one, then the encoding isn't ASCII.
Rather, it is a special implementation based on the protocol. It's not that
the encoding is wrong, it's that the protocol is doing something
non-standard.

Do you have more information as to what the protocol is expecting/doing?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

scott said:
Thx for the quick reply

I did acutlay try all the ones that exist in System.Text namespace and the
only one that worked was the ASCII one. it just did not do them special
charectors. I was just wondering if any one new a reason as to why this
was
so that i could correct the problem. If no one knows to a reason as to
why
its not doing it ill keep hunting around for an answer.

in
message news:%[email protected]...
Scott,

Why not just find out what encoding those other clients are using? The
server should be using some well-defined encoding scheme (ASCII, UTF8, or
something of that nature). When you find out what it is, just find the
corresponding .NET encoding class (in the System.Text namespace, most of
them are there), and run your bytes through that.

If it is not a well-defined encoding, then I would take the rules
that
make up how the bytes are sent/transformed and derive a class from Encoding
that encapsulates these rules.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


hi all, hope some one can help me, this prob is driving me mad.



im using sockets to communicate between a client and a server. I
don't'
have control over the client and how it sends the data so i have had to
try
and work out how it is doing it. From what i can see it is sending the
data
in ASCII because if i try to use Unicode it just stops working, where's
with
ASCII i can get response from it and send and receive data.



The problem.



The client sends to me a load of data, most of it is ok however some of
the
data contains special text like §. When i try to decode one of these
or
anything else like it the decoding just goes wrong and it reads it as
a
_.
I know that it must be possible because other people have been able to
communicate with the client and use these special characters but i
can't
seem to.



Does any one have any ideas ?



Thx scott.
 
J

Jon Skeet [C# MVP]

scott said:
Thx for your help in this. Iv managed to solve the prob.

None of the decoders worked in System.Text namespace.

for some reason i decied to try a simple cast of each byte to a char and it
worked.

Then that suggests ISO-Latin-1 is the encoding you want. You can get
that with Encoding.GetEncoding (28591).

However, you may well find that that's *not* exactly right, because of
the values between 128 and 160. You may well find it's one of the "ANSI
encodings", which may well depend on what locale the client is running
on...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top