Encoding Question

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: C# Learner (csharp_at_learner.here)
Date: 02/25/04


Date: Wed, 25 Feb 2004 16:08:49 +0000

Imagine the following scenario.

You receive a byte array from a socket. This byte array contains both
text and binary data; it contains text fields delimited by specified
byte sequences.

For example:

"one \xC0\x80 two \xC0\x80 three"

The way I'm currently dealing with this is to convert the byte array to
a string with the following function, then splitting the string by the
delimiter sub-string.

public static string GetString(byte[] data)
{
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < data.Length; ++i) {
                sb.Append((char)data[i]);
        }

        return sb.ToString();
}

I know this is a hack, but is there a better way?



Relevant Pages

  • Re: Convert from byte array to string
    ... i have no problem with the data sent to as400 socket, ... Ricardo Quintanilla G. ... >> the socket responds to me as a "byte array". ... >> then i need to convert the "byte array" into a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TCP/IP Sockets with GNAT.Sockets
    ... the problem is the use of attributes 'Output on arrays which output array bounds before array data. ... and since String is an array, String'Output outputs String bounds before string data. ... you should note that the problem is the same in C: when reading a string from a socket, ... Streams while a Socket package isn't required to give any explanations ...
    (comp.lang.ada)
  • Re: Convert from byte array to string
    ... > i have no problem with the data sent to as400 socket, ... > data received as response when i try to convert it from "byte array" to> string, ... >>> Ricardo Quintanilla G.>> ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: 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". ... > the problem is that the data converted from the byte array into a string>, ... > look at the last word, it have an accent, but when i convert it, the> accent ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: VB6 can do, C# can NOT do !!! Byte [] into string is invalid data
    ... store byte array into a string. ... the string will be returned to classic ASP, ... You shouldn't treat arbitrary binary data as text. ...
    (microsoft.public.dotnet.languages.csharp)