Is there a built in Base10 to Base64 encoder/decoder

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
istech
Posts: 211
Joined: Thu Sep 19, 2013 10:08 am

Is there a built in Base10 to Base64 encoder/decoder

Post by istech » Sun Dec 11, 2016 6:26 pm

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;
}

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

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

Post by SparkOut » Sun Dec 11, 2016 6:51 pm

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)?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10330
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Sun Dec 11, 2016 6:54 pm

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

istech
Posts: 211
Joined: Thu Sep 19, 2013 10:08 am

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

Post by istech » Sun Dec 11, 2016 7:14 pm

@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/"
Last edited by istech on Mon Dec 12, 2016 9:49 pm, edited 1 time in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

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

Post by FourthWorld » Sun Dec 11, 2016 7:52 pm

What needs to be done with the numeric value while it's in base 64 form?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

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

Post by [-hh] » Sun Dec 11, 2016 9:13 pm

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.
shiftLock happens

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

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

Post by MaxV » Mon Dec 12, 2016 5:45 pm

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.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

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

Post by [-hh] » Mon Dec 12, 2016 6:40 pm

Was a joke :-)
Last edited by [-hh] on Mon Dec 12, 2016 7:48 pm, edited 2 times in total.
shiftLock happens

istech
Posts: 211
Joined: Thu Sep 19, 2013 10:08 am

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

Post by istech » Mon Dec 12, 2016 6:41 pm

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.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

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

Post by [-hh] » Mon Dec 12, 2016 7:21 pm

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
shiftLock happens

istech
Posts: 211
Joined: Thu Sep 19, 2013 10:08 am

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

Post by istech » Mon Dec 12, 2016 8:28 pm

@ -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.

Post Reply