By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

Convert from byte array to string

P: n/a
i have a code that sends data to a socket listening over as400 platform,
the socket responds to me as a "byte array".

then i need to convert the "byte array" into a string.
the problem is that the data converted from the byte array into a string
, is not what i expect.

example:
- the data returned from the socket is (byte array)
"Usuario Sin Atribuciones Para Esta Función"

- the data obtained from the convert process is (a string)
"Usuario Sin Atribuciones Para Esta Funcisn"

look at the last word, it have an accent, but when i convert it, the accent
is loosed.
i am using then next code, in order to convert the byte array to string

-----------------------------------------------------------------------
string dataReceivedinText = Encoding.ASCII.GetString(bytesDataReceived);
-----------------------------------------------------------------------

what can i do, in order to solve my problem?
any suggestion?

many thanks
--
Ricardo Quintanilla G.
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Ricardo,

It would seem to me that you are using the wrong encoding, or you need
to specify the correct code page for the encoder. Are you sure that the
bytes being sent are ASCII encoded?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Quintanilla" <Ri****************@discussions.microsoft.com> wrote
in message news:5C**********************************@microsof t.com...
i have a code that sends data to a socket listening over as400 platform,
the socket responds to me as a "byte array".

then i need to convert the "byte array" into a string.
the problem is that the data converted from the byte array into a string
, is not what i expect.

example:
- the data returned from the socket is (byte array)
"Usuario Sin Atribuciones Para Esta Función"

- the data obtained from the convert process is (a string)
"Usuario Sin Atribuciones Para Esta Funcisn"

look at the last word, it have an accent, but when i convert it, the
accent
is loosed.
i am using then next code, in order to convert the byte array to string

-----------------------------------------------------------------------
string dataReceivedinText = Encoding.ASCII.GetString(bytesDataReceived);
-----------------------------------------------------------------------

what can i do, in order to solve my problem?
any suggestion?

many thanks
--
Ricardo Quintanilla G.

Nov 16 '05 #2

P: n/a
First all, thanks Nicholas.

i have no problem with the data sent to as400 socket, my problem is with the
data received as response when i try to convert it from "byte array" to
string, some characters are wrong converted.

i am using the NetworkStream class in order to send/receive data over a
TcpClient instance class.

how can i apply your advice?

--
Ricardo Quintanilla G.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Ricardo,

It would seem to me that you are using the wrong encoding, or you need
to specify the correct code page for the encoder. Are you sure that the
bytes being sent are ASCII encoded?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Quintanilla" <Ri****************@discussions.microsoft.com> wrote
in message news:5C**********************************@microsof t.com...
i have a code that sends data to a socket listening over as400 platform,
the socket responds to me as a "byte array".

then i need to convert the "byte array" into a string.
the problem is that the data converted from the byte array into a string
, is not what i expect.

example:
- the data returned from the socket is (byte array)
"Usuario Sin Atribuciones Para Esta Función"

- the data obtained from the convert process is (a string)
"Usuario Sin Atribuciones Para Esta Funcisn"

look at the last word, it have an accent, but when i convert it, the
accent
is loosed.
i am using then next code, in order to convert the byte array to string

-----------------------------------------------------------------------
string dataReceivedinText = Encoding.ASCII.GetString(bytesDataReceived);
-----------------------------------------------------------------------

what can i do, in order to solve my problem?
any suggestion?

many thanks
--
Ricardo Quintanilla G.


Nov 16 '05 #3

P: n/a
Ricardo,

I understand, but you are using ASCII encoding. I also think that the
code page that you are using is wrong, and that is the case. Do you know
the code page of the machine that you are on that is processing the code?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Quintanilla" <Ri****************@discussions.microsoft.com> wrote
in message news:16**********************************@microsof t.com...
First all, thanks Nicholas.

i have no problem with the data sent to as400 socket, my problem is with
the
data received as response when i try to convert it from "byte array" to
string, some characters are wrong converted.

i am using the NetworkStream class in order to send/receive data over a
TcpClient instance class.

how can i apply your advice?

--
Ricardo Quintanilla G.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Ricardo,

It would seem to me that you are using the wrong encoding, or you
need
to specify the correct code page for the encoder. Are you sure that the
bytes being sent are ASCII encoded?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Quintanilla" <Ri****************@discussions.microsoft.com>
wrote
in message news:5C**********************************@microsof t.com...
>i have a code that sends data to a socket listening over as400 platform,
> the socket responds to me as a "byte array".
>
> then i need to convert the "byte array" into a string.
> the problem is that the data converted from the byte array into a
> string
> , is not what i expect.
>
> example:
> - the data returned from the socket is (byte array)
> "Usuario Sin Atribuciones Para Esta Función"
>
> - the data obtained from the convert process is (a string)
> "Usuario Sin Atribuciones Para Esta Funcisn"
>
> look at the last word, it have an accent, but when i convert it, the
> accent
> is loosed.
>
>
> i am using then next code, in order to convert the byte array to string
>
> -----------------------------------------------------------------------
> string dataReceivedinText =
> Encoding.ASCII.GetString(bytesDataReceived);
> -----------------------------------------------------------------------
>
> what can i do, in order to solve my problem?
> any suggestion?
>
> many thanks
>
>
> --
> Ricardo Quintanilla G.


Nov 16 '05 #4

P: n/a
Hi Ricardo,

I'm guessing you are using the default code page for your system when
writing to the port.

string dataReceivedinText = Encoding.Default.GetString(bytesDataReceived);

If this does not work, you may try one of the unicode ones, or set a
codepage of your own.

Encoding e = Encoding.GetEncoding("windows-1252");
string dataReceivedinText = e.GetString(bytesDataReceived);

This page lists some encodings for Java, but I believe .Net should support
most or all of them as well.

http://java.sun.com/j2se/1.4.2/docs/...oding.doc.html

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5

P: n/a
Hi Morten
i want to give you thank by your help.
after read your answer, i tried with

Encoding e = Encoding.GetEncoding("windows-1252");
string dataReceivedinText = e.GetString(bytesDataReceived);

....and it worked good.
many thanks again.

"Morten Wennevik" wrote:
Hi Ricardo,

I'm guessing you are using the default code page for your system when
writing to the port.

string dataReceivedinText = Encoding.Default.GetString(bytesDataReceived);

If this does not work, you may try one of the unicode ones, or set a
codepage of your own.

Encoding e = Encoding.GetEncoding("windows-1252");
string dataReceivedinText = e.GetString(bytesDataReceived);

This page lists some encodings for Java, but I believe .Net should support
most or all of them as well.

http://java.sun.com/j2se/1.4.2/docs/...oding.doc.html

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #6

P: n/a
Hi,

As400 sends data in the Windows Western Code page (8 bits), where the "ó"
letter has a value of 0xF3...

On the other side you're using System.Text.Encoding.ASCII.GetString() method
that expects a 7 bits ASCII (DOS: Western Europe code page). As result, the
letter you get is 0x73 (an "s" since the value exceeds 7 bits and the MSB
was removed).

So you need to change the codepage on both machines to be the same (ie,
change the as400 code to create the byte array based on 7bits ASCII code and
loose the Upper case accented letters since they doesn't exist in that code
page) or take your time and derive you class from System.Text.Encoding to do
the work.

You can also parse the byte array before doing the conversion since almost
all characters will be the same except for those above 0x7F (only accents
and other special characters).
Good Luck!
Rafael Pereyra

"Ricardo Quintanilla" <Ri****************@discussions.microsoft.com> wrote
in message news:16**********************************@microsof t.com...
First all, thanks Nicholas.

i have no problem with the data sent to as400 socket, my problem is with
the
data received as response when i try to convert it from "byte array" to
string, some characters are wrong converted.

i am using the NetworkStream class in order to send/receive data over a
TcpClient instance class.

how can i apply your advice?

--
Ricardo Quintanilla G.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Ricardo,

It would seem to me that you are using the wrong encoding, or you
need
to specify the correct code page for the encoder. Are you sure that the
bytes being sent are ASCII encoded?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ricardo Quintanilla" <Ri****************@discussions.microsoft.com>
wrote
in message news:5C**********************************@microsof t.com...
>i have a code that sends data to a socket listening over as400 platform,
> the socket responds to me as a "byte array".
>
> then i need to convert the "byte array" into a string.
> the problem is that the data converted from the byte array into a
> string
> , is not what i expect.
>
> example:
> - the data returned from the socket is (byte array)
> "Usuario Sin Atribuciones Para Esta Función"
>
> - the data obtained from the convert process is (a string)
> "Usuario Sin Atribuciones Para Esta Funcisn"
>
> look at the last word, it have an accent, but when i convert it, the
> accent
> is loosed.
>
>
> i am using then next code, in order to convert the byte array to string
>
> -----------------------------------------------------------------------
> string dataReceivedinText =
> Encoding.ASCII.GetString(bytesDataReceived);
> -----------------------------------------------------------------------
>
> what can i do, in order to solve my problem?
> any suggestion?
>
> many thanks
>
>
> --
> Ricardo Quintanilla G.


Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.