I'm following your forum for quite a while now and usually found answers, but not this time.
I'm trying to do some networking stuff with several NXTs and therefore wanted to split a string containing flattened values.
For splitting I used SubStr() and had problems with it. MidStr() instead worked as desired. Here's an example:
Code: Select all
task main()
{
// Just a random array so I can mix up something
byte l_bTestArray[] = { 0x64, 0x4c, 0x4d, 0x01 };
// Now in a string
string l_sTestString = ByteArrayToStr(l_bTestArray);
// Now a value I would like to get back in the end
int l_iValue = 55;
// Flatten the value
string l_sFlattenedValue = FlattenVar(l_iValue);
// Combine the two strings
string l_sComplete = strcat(l_sTestString, l_sFlattenedValue);
// Still works like a charm
TextOut(0, LCD_LINE1, l_sComplete);
// Now I want the header (l_bTestArray) only
string l_sHeader = SubStr(l_sComplete, 0, 4);
// Now I want the value (my 'payload') only - therefore I do some math
// l_iLength will be 2 which is correct
int l_iLength = (StrLen(l_sComplete) - StrLen(l_sHeader));
// but the payload string will be empty with SubStr()
string l_sFlattenedPayload = SubStr(l_sComplete, StrLen(l_sHeader), l_iLength);
int l_iUnflattenedValue = 0;
UnflattenVar(l_sFlattenedPayload, l_iUnflattenedValue);
NumOut(0, LCD_LINE4, l_iUnflattenedValue);
NumOut(0, LCD_LINE5, StrLen(l_sHeader));
NumOut(0, LCD_LINE6, StrLen(l_sFlattenedPayload));
NumOut(0, LCD_LINE7, l_iLength);
Wait(15000);
}
Result with MidStr():dLM7
0
4
0
2
The guide says for SubStr():dLM7
55
4
2
2
and for MidStr():string SubStr (string str, unsigned int idx, unsigned int len)
[inline]
Extract a portion of a string. Return a sub-string from the specified input string starting
at idx and including the specified number of characters. The input string parameter
may be a variable, constant, or expression.
All in all I can't really tell the difference between the two by this description, but since they do not work the same way there must be something.string MidStr (string str, unsigned int idx, unsigned int len)
[inline]
Copy a portion from the middle of a string. Returns the substring of a specified length
that appears at a specified position in a string.
Can you help me?
Cheers,
noob4life