//We decode and extract the information from the hexagon address public BookStruct decodeHex(string hex) { string text; //The original user search string //We first test the last character if its one, //Then it's compressed. Else, its not if (hex[hex.Length - 1] == '1') { text = hex.Substring(0, hex.Length - 1); //Trim the ending text = Compressor.Unzip(Compressor.StringToByteArray(text)); //Convert hex into byte array into decompressed text } else { text = hex.Substring(0, hex.Length - 1); text = convertToBase(convertFromBase(text, hexes).ToString(), bases); //Revert back to original state } IntX seed = convertFromBase(text, bases); //Seed we used in the encode hex method RNG.setSeeds(seed, seed % answerToLifeTheUniverseAndEverything); //Because the seed is the same, and unique per user-input, we can //expect the same wall, shelf and volume to be outputted as when we //generated the values IntX e_wall = RNG.getRRandom(1, 4); IntX e_shelf = RNG.getRRandom(1, 5); IntX e_vol = RNG.getRRandom(1, 32); Random r = new Random((int)RNG.getRand()); //We generated this in the encodehex but here we generate it again //and due to the seed being the same, we should get the same results int loc = r.Next(1, 410); int locOnPage = r.Next(3200); //Dump all those information into an object BookStruct ret = new BookStruct(e_wall, e_shelf, e_vol, loc, hex) { expectedLoc = locOnPage, searchText = text, expectedPage = loc }; return(ret); }
//We encode all the infornmation into one nice hexagon address! public string encodeHex(string text) { string addr1 = Compressor.ByteArrayToString(Compressor.Zip(text)); //Convert text into byte stream into hex string addr2 = convertToBase(convertFromBase(text, bases).ToString(), hexes); //Convert text to IntX into hex string addr; //Which one is shorter? We prefer the shorter one! if (addr1.Length <= addr2.Length) { addr = addr1 + '1'; //Set the last charater to be 1 if compressed Console.WriteLine("Hexagon: " + addr); } else { addr = addr2 + '0'; //And 0 if not compressed Console.WriteLine("Hexagon: " + addr); } IntX seed = convertFromBase(text, bases); //Convert text into IntX using bases //We get the wall, shelf and volume RNG.setSeeds(seed, seed % answerToLifeTheUniverseAndEverything); IntX wall = RNG.getRRandom(1, 4); IntX shelf = RNG.getRRandom(1, 5); IntX vol = RNG.getRRandom(1, 32); //Then get the page and location on the page that the text will appear on Random r = new Random((int)RNG.getRand()); int loc = r.Next(1, 410); int locOnPage = r.Next(3200); //Display the information Console.WriteLine("Wall: " + wall); Console.WriteLine("Shelf: " + shelf); Console.WriteLine("Volume: " + vol); Console.WriteLine("Page: " + loc); Console.WriteLine("Location on Page: " + locOnPage); //And return the hexagon address return(addr); }