Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a byte array
eg
Dim ArrayBytes = {0, 5, 200, 95, 6, 11, 100, 8, 120, 0, 0, .........}

I want all selected item has three number as string (eg 5 > "005", 93 > "093", 0 > "000")
if selected item contains one number i want add front of byte "00" (eg "005")
if selected item contains two numbers i want add front of byte "0" (eg "095")

What I have tried:

Dim NewBytes2 As New List(Of String)(ArrayBytes.GetRange(0, 10).Where(Function(x) If CStr(x).lenght = 1 Then   x =  "00" & CStr(x) ElseIf CStr(x).lenght = 2 Then   x =  "0" & CStr(x) end if)
Posted
Updated 6-Apr-18 5:15am
v3
Comments
G3Coder 6-Apr-18 11:11am    
This what you are after: https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading-zeros ?
gacar 6-Apr-18 13:39pm    
Yes there is about my question. Thank you for your comment.

1 solution

Try:
VB
Dim arrayBytes As Byte() = New Byte() {0, 5, 200, 95, 6, 11, 100, 8, 120, 0, 0}
Dim withZeros As String() = arrayBytes.[Select](Function(b) b.ToString("000")).ToArray()
 
Share this answer
 
Comments
Bryian Tan 6-Apr-18 11:42am    
nice ++ :)
gacar 6-Apr-18 13:32pm    
Thank you. This is working
gacar 6-Apr-18 14:07pm    
Last thing, is it posible to string? (not string array())
OriginalGriff 6-Apr-18 14:21pm    
What - exactly - do you want in the string?
gacar 6-Apr-18 14:29pm    
Yes. But thank you. I found solution from stackoverflow.
Dim withZeros = arrayBytes.Select(Function(b) b.ToString("000")).Aggregate(Function(x, y) x & ", " & y)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900