473,230 Members | 1,729 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,230 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 10083
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...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.