Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If I have multiple (16) values in string, how can I get first 4 values? I think I need to parse this string and starting from null, 1, 2, 3, 4, null. please advise if I am correct.


Program Send : string = "00 01 02 03 04 05 05 06 04 04 etc "
i want only starting 4 values in string = "00 01 02 03 04"

What I have tried:

byte[] ScanModeData = new byte[40960];
           int ValidDatalength, i;
           string temp, temps;
           ValidDatalength = 0;

           fCmdRet = StaticClassReaderB.ReadActiveModeData(ScanModeData, ref ValidDatalength, frmcomportindex);
           if (fCmdRet == 0)
           {
               temp = "";
               temps = ByteArrayToHexString(ScanModeData);

               for (i = 0; i < ValidDatalength; i++)
               {
                   temp = temp + temps.Substring(i * 2, 2) + " ";


               }
Posted
Updated 4-Mar-17 22:35pm
v2
Comments
Maciej Los 5-Mar-17 3:28am    
You should provide sample string.
Yugal Kishor 5-Mar-17 3:59am    
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
for (int i = 0; i < data.Length; i++)
{
sb.Append(Convert.ToString(data[i], 16).PadLeft(2, '0'));
}
return sb.ToString().ToUpper();
}
Patrice T 5-Mar-17 4:20am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
OriginalGriff 5-Mar-17 3:57am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind. So give us an example of your input string, and also show us what you expect as an output.
Use the "Improve question" widget to edit your question and provide better information.
Yugal Kishor 5-Mar-17 3:59am    
private string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder(data.Length * 3);
for (int i = 0; i < data.Length; i++)
{
sb.Append(Convert.ToString(data[i], 16).PadLeft(2, '0'));
}
return sb.ToString().ToUpper();
}

1 solution

If you have a common marker ("xxx,xxx,xxx" - comma in this example), then use String.Split Method[^].

But without an example, we have no idea what you are asking for.

As OriginalGriff mentioned, try it (rather than get others to do it for you), you might surprise yourself! It is call programming and learning through research - it is how we learned.
 
Share this answer
 

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