Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Covert Plain text to Base64 format using Javascript How?

    Hello Javascript Developers.

    I have one requirement for Convert Plain text to base 64 format ,How can I do that using javascript. because I need to write the contents to one notepad .it needs to be written after encoding. please help me to get that.

    Thanks and regards
    Murali

  • #2
    New Coder
    Join Date
    Aug 2013
    Posts
    50
    Thanks
    0
    Thanked 11 Times in 11 Posts
    Google it. I'm sure there are many solutions already.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Quote Originally Posted by jscriptuser View Post
    Hello Javascript Developers.

    I have one requirement for Convert Plain text to base 64 format ,How can I do that using javascript. because I need to write the contents to one notepad .it needs to be written after encoding. please help me to get that.

    Thanks and regards
    Murali
    Here you are:-

    Code:
    <script type = "text/javascript">
    
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    
    function encode64(input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    
    while (i < input.length) {
    chr1 = input.charCodeAt(i++);
    chr2 = input.charCodeAt(i++);
    chr3 = input.charCodeAt(i++);
    
    enc1 = chr1 >> 2;
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    enc4 = chr3 & 63;
    
    if (isNaN(chr2)) {
    enc3 = enc4 = 64;
    }
    	
    else if (isNaN(chr3)) {
    enc4 = 64;
    }
    
    output+=keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
    }
    	
    return output;
    }
    
    //document.write(encode64("abc123"));
    alert (encode64("abc123"));
    
    
    </script>
    I suppose that what we are all waiting for is a picture of Kate with the baby coming out. - ITV Reporter

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    hgs
    hgs is offline
    New Coder
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    81
    Thanks
    3
    Thanked 5 Times in 5 Posts
    My site
    If you’re doing software development right, you’re probably doing Agile wrong.
    -- Isaac Schlueter

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    native:
    Code:
    btoa("hello world")
    if you need to support old browsers or handle more than thumbnail-amounts of data, it REALLY pays to shop around.
    some user-land routines are literally 200X faster than others in jscript, and some of them don't like unicode or binary very much.

    i won't endorse or condemn any prior suggestions, but you should try a few different ones in your project to see what's best.
    Last edited by rnd me; 08-11-2013 at 11:33 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •