Essas funções fazem um número grande e compacta em uma string pequena.
Exemplo: com a função base256.cod 439804715386 fica assim: ffgaz
Usando a função base256.decod, ffgaz é restaurado para o número 439804715386.
Copie o código abaixo e salve como base256.pris (use inclua'base256' )
/*
biblioteca prisma para codificar um numero em base 256 e 10
um número extenso como 989238498234 pode ocupar apenas quatro cacacteres assim
economizando espaços.
linguagemprisma@gmail.com
linguagemprisma.br4.biz/blog
Adalberto set/2017
Licença MIT (use como bem quiser!):
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
local tab_insira = tabela.insira;
local mat_corte = mat.corte;
local str_car, str_cod = string.car, string.cod;
local tab_desempacote = tabela.desempacote;
local funcao reverta(tab)
local ret = {};
local cont = 1;
para i=#tab,1,-1 inicio
ret[cont] = tab[i];
cont = cont + 1;
fim
retorne ret;
fim
local funcao base10_256(b10)
local b10 = b10 ou 0;
local tmpa,tmpb = b10,0;
local tab = {};
enquanto 1 inicio
tmpb = tmpa % 256; //resto.
tmpa = mat_corte(tmpa / 256);//pega so a parte interia da divisao.
tab[#tab+1] = tmpb;
se tmpa == 0 entao quebre; fim
fim
retorne reverta(tab);
fim
local funcao base256_str(b256)
local tmp = '';
tmp = str_car(tab_desempacote(b256));
retorne tmp;
fim
local funcao base256_10(a,...)
local tab;
se tipo(a) == 'tabela' entao tab = a; senao
tab = {a,...};
fim
local tmp = tab[#tab];
local cont = 1;
para i = #tab - 1,1,-1 inicio
tmp = tmp + tab[i]*256^cont;
cont = cont + 1;
fim
retorne tmp;
fim
local base256 = {};
funcao base256.cod (b10)
se tipo(b10) <> 'numero' entao retorne falso,'Parâmetro #1 função cod() deve ser uma número'; fim
local ret = base10_256(b10);
retorne base256_str(ret);
fim
funcao base256.decod(str256)
se tipo(str256) <> 'string' entao retorne falso,'Parâmetro #1 função decod() deve ser uma string'; fim
retorne base256_10(str_cod(str256,1,#str256));
fim
Exemplo de uso:
local num = 91234567891234; //14 digitos são codificados em uma string de 6 elementos.
local str256 = base256.cod(num);
imprima('base256 -->', str256 );
imprima('base10-->', base256.decod(str256) );
local a = 'ffgaz';
imprima( a, '- em base10-->', base256.decod(a) );//5 letras se transforma em um numero de 12 digitos.
Falou, até mais.