Main Site Documentation

Byte arrays and strings


#1

Hi All,

Just wondering if there is an easy/preferred way using the .netmf libraries to convert a bytestream (From a http get request) into a string.

For contexts the code so far: (Thanks for the networking documentation :wink: )

 Private Sub Login()
            ClearScreen(Colors.White)
            Dim img As Bitmap = LoadImage("connecting.bmp")
            DrawBitmap(img, 110, 5)
            DrawText("Please wait " & _username & " Connecting you...", 130, 10, GT.Color.Blue)

            Dim result As Byte() = New Byte(65536) {}
            Dim read As Integer = 0

            Using req As WebRequest = HttpWebRequest.Create(_baseURI & "TcibCusLists()")
                req.ContentType = "application/json"
                Dim authInfo As String = _username & ":" & _password
                authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo))
                req.Headers.Add("Authorization", "Basic " & authInfo)
                Try
                    Using res = req.GetResponse()
                        Using readStream = res.GetResponseStream
                            Do
                                read = readStream.Read(result, 0, result.Length)
                                ' Do Decode
                                Thread.Sleep(20)
                            Loop While read <> 0

                        End Using
                    End Using
                Catch ex As Exception
                    FatalError("Login", ex.Message)
                End Try

            End Using
            Debug.Print("Connected...")
        End Sub

#2

I think String has an constructor that takes a byte array.
Also StringBuilder might have an Append for byte array, if you need to concat multiple parts.
Please be aware, that in NETMF only UTF8 strings are supported, so any byte array would be expected to be UTF8 data.
If you have ASCII data in the byte array, I would do like this:

var sb = new StringBuilder(array.Length);
foreach(var b in array)
{
   sb.Append((char)b);
}
var str = sb.ToString();

#3

Thanks,

I can adjust the content type to do this:



I'll try later.

Cheers,

Jas

#4

@ jasuk70 - Sorry I don’t have a VB version, but you can see how I do it in C#:

EDIT: These were done as extension methods. They would be used like this:

byte[] byteResults = new byte[200];
string stringResults = byteResults.ToString();
string stringResults2 = byteResults.ToString(100, 5);


		/// <summary>
		/// Outputs a UTF8 string converted from all bytes in this array.
		/// </summary>
		/// <param name="offset">Start index</param>
		/// <param name="length">Number of bytes to process</param>
		/// <returns>A string converted from all bytes in this array.</returns>
		public static string ToString(this byte[] array)
		{
			if (array == null || array.Length == 0)
				return String.Empty;

			return new string(Encoding.UTF8.GetChars(array));
		}

		/// <summary>
		/// Outputs a UTF8 string converted from a subset of bytes in this array.
		/// </summary>
		/// <param name="offset">Start index</param>
		/// <param name="length">Number of bytes to convert.</param>
		/// <returns>A string converted from bytes in this array.</returns>
		public static string ToString(this byte[] array, int offset, int length)
		{
			if (array == null || array.Length == 0 || array.Length < (offset + length))
				return String.Empty;

			return new string(Encoding.UTF8.GetChars(array, offset, length));
		}