locked
Convert byte [] to String? RRS feed

  • Question


  • Hi, i'm programing a game for pocket pc using a smart device aplication in visual studio c#  2003...

    I'm trying to convert a byte [] to a string, and nothing....
    I search in the internet, and i get this solution:

    Byte [] array2 = new byte [100];
    .....
    string final = System.Text.ASCIIEncoding.ASCII.GetString(array2);


    and it returns an error: No overload for method 'GetString' takes '1' arguments, because the method have 3 inputs... and i only put 1... but using windows aplication to test, this works with only 1 argument.... when i try to put the 3, didn't do what i wanted...

    My problem is, i have a buffer (byte [] ) with this format:
    [int string_size, string,int string_size, string,int string_size, string....], and the follow code to read it:

    int aux2 = 0;
    byte [] aux = new byte [100];
    for(int i =0; i<n_pecas;i++)
                        {                   

                            aux = new byte[4];
                            System.Array.Copy(receive_message,aux2,aux,0,4);
                            int size = BitConverter.ToInt32(aux, 0);
                            aux2+=4;

                            aux = new byte[size];
                            System.Array.Copy(receive_message,aux2,aux,0,size);
      
        //This works in windows aplication, but in smart device gets the error above
                  string path = System.Text.ASCIIEncoding.ASCII.GetString(aux);
       
        // This doesn t work in both, i don t know, what to put index or in the count, the index should be 0, the      //count i dont know...
                            string path = System.Text.ASCIIEncoding.ASCII.GetString(aux,0,0);//Wrong index or wrong count...
                           

        aux2+=size ;
                            MessageBox.Show("Path: "+path);
    }

    if someone could help me, putting this code correct i appreciate...or making a similar code, like the one i use to convert a string to byte [] :

    public byte[] StrToByteArray(string str)
            {
                byte[] byteArray = new Byte[str.Length];
                for (int index=0; index < str.Length; index++)
                {
                    byteArray[index] = (byte)str[index];
                }
                return byteArray;
            }







    Hi, sorry if this this post is in the wrong place....
    Monday, December 10, 2007 7:54 PM

Answers

  • Encoding.GetString(Byte[]) is not supported on .NET CF.  Use Encoding.GetString(Byte[], Int32, Int32) instead.  For example:

     

    Code Block

    string final = System.Text.ASCIIEncoding.ASCII.GetString(array2, 0, array2.Length);

     

     

    Monday, December 10, 2007 8:20 PM

All replies

  • Encoding.GetString(Byte[]) is not supported on .NET CF.  Use Encoding.GetString(Byte[], Int32, Int32) instead.  For example:

     

    Code Block

    string final = System.Text.ASCIIEncoding.ASCII.GetString(array2, 0, array2.Length);

     

     

    Monday, December 10, 2007 8:20 PM
  • Thank you very much, that works... Smile
    Monday, December 10, 2007 8:25 PM