Convert from byte array to string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,

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?
 
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 said:
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]
- (e-mail address removed)

Ricardo Quintanilla said:
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,

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]
- (e-mail address removed)

Ricardo Quintanilla said:
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 said:
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]
- (e-mail address removed)

"Ricardo Quintanilla" <[email protected]>
wrote
in message news:[email protected]...
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
 
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/guide/intl/encoding.doc.html
 
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.
 
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 said:
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 said:
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]
- (e-mail address removed)

"Ricardo Quintanilla" <[email protected]>
wrote
in message news:[email protected]...
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
 
Back
Top