Passing parameters with a single string

By Aug 06, 2015

Description:

Passing parameters with a single string

Preferencesoft

It may happen that a program or a function must return several parameters in one shot.

One possible method is to return all these data in the form of a single string.

If you want to return multiple strings, then a problem arises: how to form a string from them as it is possible to find again each of them?

A solution is to prohibit strings contain a character for example newline n and it concatenates the strings together, inserting this character. But it gets more complicated if you want to be able to return strings that can contain any character.

You can book another character % for example and replace in all strings some characters by a sequence of characters:

% by %0

\n by %1

Etc.

It will then be easy to reconstruct the original strings.

You can also concatenate all string without separator and add at the beginning of the string the information in the form of characters giving the length of each string.

This method has the advantage of not to modify any of the strings. On the other hand, it is necessary to add at the beginning of it supplementary characters.

We agree to place at the beginning of the chain of the result, integers encoded as base64.

The first integer represents the number of parameters, i.e., the number of strings. This integer will be followed by the same number of integers than the number of parameters. These correspond to the lengths of each string.

For simplicity, these integers will be represented in base 64 by a string of 6 characters (fixed size).

Here is an implementation of the algorithm in C#:

static int[] characters_value = new int[] {
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,62,0,0,0,63,0,1,
  2,3,4,5,6,7,8,9,0,0,
  0,0,0,0,0,10,11,12,13,14,
  15,16,17,18,19,20,21,22,23,24,
  25,26,27,28,29,30,31,32,33,34,
  35,0,0,0,0,0,0,36,37,38,
  39,40,41,42,43,44,45,46,47,48,
  49,50,51,52,53,54,55,56,57,58,
  59,60,61,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0,0,0,0,0,
  0,0,0,0,0,0};
 
 char[] base64_characters = new char[] {
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  'U', 'V', 'W', 'X', 'Y', 'Z',
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  'u', 'v', 'w', 'x', 'y', 'z',
  '+', '/'};
 
 public string IntToBase64(int i)
 {
     char[] c = new char[6];
     int j = i;
     c[5] = base64_characters[(j & 63)];
     j >>= 6;
     c[4] = base64_characters[(j & 63)];
     j >>= 6;
     c[3] = base64_characters[(j & 63)];
     j >>= 6;
     c[2] = base64_characters[(j & 63)];
     j >>= 6;
     c[1] = base64_characters[(j & 63)];
     j >>= 6;
     c[0] = base64_characters[(j & 63)];
     return new string(c);
 }
 
 public int Base64ToInt(char c0, char c1, char c2, char c3, char c4, char c5)
 {
     return (characters_value[c0] << 30) + (characters_value[c1] << 24) + (characters_value[c2] << 18) +
            (characters_value[c3] << 12) + (characters_value[c4] << 6) + characters_value[c5];
 }
 
 public string DataConcat(string[] a_s)
 {
     int n = a_s.Length;
     int dep = 6 * (n + 1);
     string s = "";
     string s_b = IntToBase64(n);
     for (int i = 0; i < n; i++)
     {
         int l = a_s[i].Length;
         s_b += IntToBase64(l);
         s += a_s[i];
     }
     return s_b + s;
 }
 
 public string[] DataSplit(string s)
 {
     int n = Base64ToInt(s[0], s[1], s[2], s[3], s[4], s[5]);
     string[] a_s = new string[n];
     int j = 6;
     int b = 6 * n + 6;
     for (int i = 0; i < n; i++)
     {
         int l = Base64ToInt(s[j], s[j + 1], s[j + 2], s[j + 3], s[j + 4], s[j + 5]);
         j += 6;
         a_s[i] = s.Substring(b, l);
         b += l;
     }
     return a_s;
 }
 
 private void button_Click(object sender, RoutedEventArgs e)
 {
     string[] ss = DataSplit(DataConcat(new string[] { "A", "B", "55555", "123456789" }));
     string s = "";
     for (int i = 0; i < ss.Length; i++)
     {
         s += "; " + ss[i];
     }
     textBlock.Text = s;
 }

 

For integer or real parameters, can convert to strings of characters or encode in base 64.

For complex types such as tables, lists, records, it is necessary to choose a representation of the data. Simple data can be easily converted to character strings.

A library to do the marshaling exists in C#, but this program is not intended to transmit any data type.

 


CSharp

Categories

Share

Follow


KodFor Privacy Policy