Page 1 of 1

Is there a built in Base10 to Base64 encoder/decoder

Posted: Sun Dec 11, 2016 6:26 pm
by istech
Hi Livecoder,

Got a small issue. I want to convert a base64 string to Base10. However the "baseconvert()" does not cover b64 to b10 or vice versa(In the dictionary). Is there a solution other then writing a function to do it?

I have done a quick web search and found some php versions. If there is not solution maybe someone could help convert them to livecode for all to use?

Thanks all

Code: Select all

$rep = array('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','-','_');
$new = "";
while ($num>0) {
    $r = $num % 64;
    $new .= $rep[$r];
    $num = floor($num/64);
}


function lengthen($id) {
    $alphabet='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';

    $number=0;
    foreach(str_split($id) as $letter) {
        $number=($number*64) + strpos($alphabet,$letter);
    }
    return $number;
}

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Sun Dec 11, 2016 6:51 pm
by SparkOut
Have you looked at base64Encode and base64Decode?
base64Decode will return the original binary data that was base 64 encoded. So not directly converting to "base 10" but surely more likely (eg an image or other attachment to an email)?

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Sun Dec 11, 2016 6:54 pm
by dunbarx
HI.

Do you really mean a number expressed in base 64? Fascinating. What characters are used to express each "digit"? After all, base 16 used "A-F", but base 64 would require 28 beyond "Z".

I bet I have this wrong. It has nothing to do with base64Encode?

Craig Newman

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Sun Dec 11, 2016 7:14 pm
by istech
@Sparkout-Yes, it has to be a number. So the base64Encode()/Decode does not work for my situation.

@dunbarx-Yes, There are many online converters that do it for you. I just need a Livecode version. "https://conv.darkbyte.ru/"

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Sun Dec 11, 2016 7:52 pm
by FourthWorld
What needs to be done with the numeric value while it's in base 64 form?

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Sun Dec 11, 2016 9:13 pm
by [-hh]
LiveCode uses the standard of base64-encoding symbols A-Z,a-z,0-9 and "+/".
Zero ore more "=" indicate the end of the code, this is a 'filler' char.

It is what 'SparkOut' (indirectly) said:

base64Encode(base256-input) encodes base 256 to base 64,
base64Decode(base64-input) decodes base 64 to base 256.


That is, if your input is a decimal number (what it is also in base 256) and correctly encoded, then it decodes correctly back to that decimal number.

Example.
base64Encode("233456709876") is "MjMzNDU2NzA5ODcz",
base64Decode("MjMzNDU2NzA5ODcz") is "233456709876".

Be very cautious with online converters as long as they don't document the symbols.
The standard is A-Z,a-z,0-9 and "+/", in that order (26+26+10+2 symbols).

Your php code uses as last symbols "-_" and for the first 62 coding chars a different order!!

A good explanation is here: https://en.wikipedia.org/wiki/Base64
See also the LC dictionary with a link to RFC 2045.

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Mon Dec 12, 2016 5:45 pm
by MaxV
istech wrote:Hi Livecoder,

Got a small issue. I want to convert a base64 string to Base10. However the "baseconvert()" does not cover b64 to b10 or vice versa(In the dictionary). Is there a solution other then writing a function to do it?
This is the code you need to see a base 64 text:
########CODE#######
on mouseUp
put field 1 into temp #base64
put 0 into cont
repeat for each char tChar in temp
add 1 to cont
put tChar after tempStr
if cont = 4 then
put 0 into cont
converti tempStr
put empty into tempStr
end if
end repeat
end mouseUp

on converti temp
put base64decode(temp) after field 2
end converti
#####END OF CODE#####

Example:

Code: Select all

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
becomes

Code: Select all

Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Mon Dec 12, 2016 6:40 pm
by [-hh]
Was a joke :-)

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Mon Dec 12, 2016 6:41 pm
by istech
Hi all,

I think we are drifting away from what I need. So I will provide a example.

You have a base64 encoded information "AwIDBA==" (translate to binary below)
You have the binary translation per byte "00000011 00000010 00000011 00000100"
Which gives you the decimal "3 2 3 4"

00000011 = 3 in decimal
00000010 = 2 in decimal
00000011 =3 in decimal
00000100 = 4 in decimal

So the decimal I get at the end I can store. (3 2 3 4)

This is exactly what I need to do in livecode. I am toying with binarydecode/binaryencode and mapping the bytes manually. Maybe that is the wrong way to go. If someone can suggest a better route that is great.

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Mon Dec 12, 2016 7:21 pm
by [-hh]
What you want is a decoding: base 64 -> tmp -> base 10 _of each byte_ of tmp.

We already know: base64Decode decodes from base 64 to base 256.
Now you have to decode each byte from base 256 to base 10. This does byteToNum.

That is: base64Decode(string) -> tmp -> byteToNum for each byte of tmp:

Code: Select all

on mouseUp
   put base64Decode(fld "IN64") into tmp
   repeat for each byte b in tmp
      put comma & byteToNum(b) after tmp -- put into items
   end repeat
   put char 2 to -1 of tmp into fld "OUT10"
 end mouseUp
The reverse is: numToByte for each item of string -> tmp -> base64Encode(tmp):

Code: Select all

on mouseUp
   put fld "IN10" into s
   repeat for each item i in s
      put numToByte(i) after tmp
   end repeat
   put base64Encode(tmp) into fld "OUT64"
end mouseUp

Re: Is there a built in Base10 to Base64 encoder/decoder

Posted: Mon Dec 12, 2016 8:28 pm
by istech
@ -hh You pretty much nailed it. Thanks a million. BytetoNum/NumtoByte never used it before but that was the magic function I needed. Thanks again for your time and much appreciated for others trying to translate what I wanted. Thanks for making this a great forum all.