DMeville

Untitled

Feb 1st, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. string DecodeMessageType(int code){
  2.     switch(code){
  3.         case 0: return "roomopen";
  4.         case 1: return "roomclose";
  5.     ///...etc
  6.     }
  7. }
  8.  
  9. int EncodeMessageType(string sCode){
  10.     switch(sCode){
  11.         case "roomopen": return 0;
  12.         case "roomclose": return 1;
  13. ///...etc
  14.     }
  15. }
  16.  
  17. EncodeMessageType("roomopen"); //returns 0, as a 16bit int
  18. DecodeMessageType(0); //returns "roomopen" as string.
  19.  
  20. //so before you send the data, Encode it to an int, and when you recieve it, before you read it decode it back to a string
  21.  
  22. //you could even go one step further and register each message type and let it figure out the int value for it automatically, instead of having to add a new case in Decode/EncodeMessageType
  23. List<string> messageTypes = new List();
  24.  
  25. void RegisterMessage(string sCode){
  26.     messageTypes.add(sCode);
  27. }
  28.  
  29. int EncodeMessageType(string sCode){
  30.     return messageTypes.indexOf(sCode);
  31. }
  32.  
  33. string DecodeMessageType(int code){
  34.     return messageTypes[code);
  35. {
Advertisement
Add Comment
Please, Sign In to add comment