473,699 Members | 3,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 dataReceivedinT ext = Encoding.ASCII. GetString(bytes DataReceived);
-----------------------------------------------------------------------

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

many thanks
--
Ricardo Quintanilla G.
Nov 16 '05 #1
6 10206
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.co m

"Ricardo Quintanilla" <Ri************ ****@discussion s.microsoft.com > wrote
in message news:5C******** *************** ***********@mic rosoft.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 dataReceivedinT ext = Encoding.ASCII. GetString(bytes DataReceived);
-----------------------------------------------------------------------

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.co m

"Ricardo Quintanilla" <Ri************ ****@discussion s.microsoft.com > wrote
in message news:5C******** *************** ***********@mic rosoft.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 dataReceivedinT ext = Encoding.ASCII. GetString(bytes DataReceived);
-----------------------------------------------------------------------

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.co m

"Ricardo Quintanilla" <Ri************ ****@discussion s.microsoft.com > wrote
in message news:16******** *************** ***********@mic rosoft.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.co m

"Ricardo Quintanilla" <Ri************ ****@discussion s.microsoft.com >
wrote
in message news:5C******** *************** ***********@mic rosoft.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 dataReceivedinT ext =
> Encoding.ASCII. GetString(bytes DataReceived);
> -----------------------------------------------------------------------
>
> 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 dataReceivedinT ext = Encoding.Defaul t.GetString(byt esDataReceived) ;

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

Encoding e = Encoding.GetEnc oding("windows-1252");
string dataReceivedinT ext = e.GetString(byt esDataReceived) ;

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.GetEnc oding("windows-1252");
string dataReceivedinT ext = e.GetString(byt esDataReceived) ;

....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 dataReceivedinT ext = Encoding.Defaul t.GetString(byt esDataReceived) ;

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

Encoding e = Encoding.GetEnc oding("windows-1252");
string dataReceivedinT ext = e.GetString(byt esDataReceived) ;

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.Enc oding.ASCII.Get String() 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.Enc oding 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************ ****@discussion s.microsoft.com > wrote
in message news:16******** *************** ***********@mic rosoft.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.co m

"Ricardo Quintanilla" <Ri************ ****@discussion s.microsoft.com >
wrote
in message news:5C******** *************** ***********@mic rosoft.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 dataReceivedinT ext =
> Encoding.ASCII. GetString(bytes DataReceived);
> -----------------------------------------------------------------------
>
> 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
15318
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 the another client in C#. Server side can not be changed :( So, I am using tcp/ip sockets. In the end I get byte on the client side, which originally was some C++ class object, converted to byte array.
2
21806
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 I do this? I don't see any conversion methods. jim
4
33130
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
9605
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 "System.Byte". How do I do this with System.Convert.ToString()???
5
4194
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
29718
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 BitConverter, I get 04030201.
6
53715
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 do I convert it to: dim myStr as string = "11,22,33,44"
7
57407
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 the data I wanted into string. I guess it should be a rather simple question.
0
2416
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 string for my app's purposes? I am using php 5.
18
43079
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 byte array (byte). Can anyone tell me how to convert byte array to byte pointer in CSharp? Please show me an simple example about how to do it. Thank you for your help
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9187
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8894
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7776
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.