Could you, please, comment this code for me, cause I want to write an opposite function, which will convert osc string's parameters into hex, which would be then sent to the surface's LCD. I want to write it myself, but don't quite understand what's happening inside the function.
Code:
function format_hex(str_in) local(str_out, pos, len) (
strcpy(str_out=#,"");
len=strlen(str_in);
pos=0;
while (pos<len) (
strcat(str_out,sprintf(#,"%02x ",str_getchar(str_in,pos)));
pos+=1;
);
str_setlen(str_out,strlen(str_out)-1); // returns str_out
);
Code:
strcpy(str_out=#,"");
This line copies str_in contents into temp string, right?
Code:
len=strlen(str_in);
This one counts the number of characters in str_in and used as the end of the while cycle?
This one sets the initial position for while cycle?
Code:
strcat(str_out,sprintf(#,"%02x ",str_getchar(str_in,pos)));
This one I don't quite understand, tbt. I feel like it's concatenating characters from temp string, one by one, to str_out string, converting them into (?) hex, cause %x is used in sprintf to convert into hex?
Iterating through the characters of the temp string "#", till the number matches len?
Code:
str_setlen(str_out,strlen(str_out)-1); // returns str_out
Why do we need to use str_setlen function? Isn't str_out length already known to us and it should be 2(4?) times smaller than srt_in? Why -1?