473,378 Members | 1,369 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,378 software developers and data experts.

Convert from byte array to string

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
6 10110
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing...
2
by: Jim H | last post by:
I am getting binary data form a database binary(8). I am copying it to a byte array byte. I need to covert this to a number, ulong, so I can write it to a string in hex format. Any idea on how...
4
by: Dan | last post by:
I need to convert a byte array to a string in order to upload a binary file with an httpWebRequest. What's the most efficient way to do such a conversion?
2
by: Dave | last post by:
Hi, I'm trying to convert a byte array to string --This works... System.BitConverter.ToString(bytes) "EB-55-79-20-18-B2-76-4D-85-0A-93-6B-97-33-31-B8" --This doesn't, but returns...
5
by: Terry Olsen | last post by:
Looking for info on how to convert a byte array to a string, and string to byte array. Thanks.
14
by: Charles Law | last post by:
I thought this had come up before, but I now cannot find it. I have a byte array, such as Dim a() As Byte = {1, 2, 3, 4} I want to convert this to an Int32 = 01020304 (hex). If I use...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
7
by: EOS | last post by:
Hi, I would like to ask on how to convert array of bytes, Byte into String? I tried StringIwant = System.Convert.ToString(byteData); but it actually return "System.Byte" rather than convert...
0
by: Sergei Shelukhin | last post by:
Hi. I receive a byte array that contains utf-16 string (with a lot of \0 characters :)) from a legacy system that holds some data that is changed elsewhere. How do I convert it to an actual utf-16...
18
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.