Hướng dẫn encode base64 javascript - mã hóa javascript base64

Bài viết / Web / Javascript / Web / Javascript

Show

Thứ ba, 15/09/2020 | 00:00 GMT+7


Mã hóa và giải mã một chuỗi trong Base64 bằng JavaScript có thể khá tiện dụng.

Vì vậy, ta có các

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
2 và
//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
3 . Hai hàm trợ giúp base64 này là một phần cốt lõi của thông số kỹ thuật HTML và có sẵn trong tất cả các trình duyệt hiện đại.

  • //Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
    // Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
    var b = new Buffer('JavaScript');
    // If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
    // We can make it convert to other formats by passing the encoding type to toString().
    var s = b.toString('base64');
    
    2 mã hóa thành Base64
  • //Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
    // Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
    var b = new Buffer('JavaScript');
    // If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
    // We can make it convert to other formats by passing the encoding type to toString().
    var s = b.toString('base64');
    
    3 giải mã từ Base64
// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"

Copy paste các ví dụ ở trên vào Control panel Chrome của bạn để xem chúng hoạt động.

Các trường hợp sử dụng phổ biến cho Base64

  • Đóng gói những thứ như đầu vào biểu mẫu và chuỗi JSON thành một bộ ký tự an toàn hơn để sử dụng trong các yêu cầu HTTP
  • Biểu diễn dữ liệu binary theo cách tương thích với HTML / JavaScript / CSS. - Ví dụ: bạn có thể nhúng một hình ảnh vào dòng trong file CSS hoặc JavaScript bằng cách sử dụng Base64.

Base64 không phải là gì:

  • Nó không nghĩa là một phương pháp mã hóa an toàn,
  • Nó không phải là một phương pháp nén, mã hóa một chuỗi thành Base64 thường dẫn đến kết quả dài hơn ~ 33%.

Tags:


Các tin liên quan

Lời hứa của JavaScript dành cho người giả 2020-09-15 Sao chép các đối tượng trong JavaScript 2020-09-15 Cách sử dụng API tìm nạp JavaScript để lấy dữ liệu 2020-09-15 Toán tử đơn nguyên JavaScript: Đơn giản và hữu ích 2020-09-15 Hiểu Hoisting trong JavaScript 2020-09-15 5 Mẹo để Viết Điều kiện Tốt hơn trong JavaScript 2020-09-15 Hiểu Vòng lặp sự kiện, Gọi lại, Hứa hẹn và Không đồng bộ / Chờ đợi trong JavaScript 2020-09-10 Bốn phương pháp để tìm kiếm thông qua các mảng trong JavaScript 2020-09-09 Sử dụng phương thức Array.find trong JavaScript 2020-09-09 split () Phương thức chuỗi trong JavaScript 2020-09-09
2020-09-15
Sao chép các đối tượng trong JavaScript
2020-09-15
Cách sử dụng API tìm nạp JavaScript để lấy dữ liệu
2020-09-15
Toán tử đơn nguyên JavaScript: Đơn giản và hữu ích
2020-09-15
Hiểu Hoisting trong JavaScript
2020-09-15
5 Mẹo để Viết Điều kiện Tốt hơn trong JavaScript
2020-09-15
Hiểu Vòng lặp sự kiện, Gọi lại, Hứa hẹn và Không đồng bộ / Chờ đợi trong JavaScript
2020-09-10
Bốn phương pháp để tìm kiếm thông qua các mảng trong JavaScript
2020-09-09
Sử dụng phương thức Array.find trong JavaScript
2020-09-09
split () Phương thức chuỗi trong JavaScript
2020-09-09

answer

234

Hướng dẫn encode base64 javascript - mã hóa javascript base64

Một số trình duyệt như Firefox, Chrome, Safari, Opera và IE10 + có thể xử lý Base64 nguyên bản. Hãy xem câu hỏi Stackoverflow này . Nó đang sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
2và các
//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
3chức năng .

Đối với JavaScript phía máy chủ (Node), bạn có thể sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
8s để giải mã.

Nếu bạn đang tìm kiếm một giải pháp trình duyệt chéo, có các thư viện hiện có như CryptoJS hoặc mã như:

http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html

Với cái thứ hai, bạn cần phải kiểm tra kỹ lưỡng chức năng về khả năng tương thích trên nhiều trình duyệt. Và lỗi đã được báo cáo .

234 hữu ích 4 bình luận chia sẻ 4 bình luận chia sẻ

answer

74

Một số trình duyệt như Firefox, Chrome, Safari, Opera và IE10 + có thể xử lý Base64 nguyên bản. Hãy xem câu hỏi Stackoverflow này . Nó đang sử dụng //Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. // Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex var b = new Buffer('JavaScript'); // If we don't use toString(), JavaScript assumes we want to convert the object to utf8. // We can make it convert to other formats by passing the encoding type to toString(). var s = b.toString('base64'); 2và các //Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. // Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex var b = new Buffer('JavaScript'); // If we don't use toString(), JavaScript assumes we want to convert the object to utf8. // We can make it convert to other formats by passing the encoding type to toString(). var s = b.toString('base64'); 3chức năng .

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"

Đối với JavaScript phía máy chủ (Node), bạn có thể sử dụng //Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. // Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex var b = new Buffer('JavaScript'); // If we don't use toString(), JavaScript assumes we want to convert the object to utf8. // We can make it convert to other formats by passing the encoding type to toString(). var s = b.toString('base64'); 8s để giải mã.

Nếu bạn đang tìm kiếm một giải pháp trình duyệt chéo, có các thư viện hiện có như CryptoJS hoặc mã như:


Với cái thứ hai, bạn cần phải kiểm tra kỹ lưỡng chức năng về khả năng tương thích trên nhiều trình duyệt. Và lỗi đã được báo cáo .

234 hữu ích 4 bình luận chia sẻ

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');

Internet Explorer 10+

var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();

Qua trình duyệt

Thư viện / mô-đun mã hóa và giải mã Javascript UTF-8 và Base64 được viết lại và mô-đun hóa cho AMD, CommonJS, Nodejs và Trình duyệt. Tương thích nhiều trình duyệt.

var str = dojox.encoding.base64.encode(myByteArray);

với Node.js

var bytes = dojox.encoding.base64.decode(str)

Đây là cách bạn mã hóa văn bản bình thường thành base64 trong Node.js:



angular
    .module('myApp', ['base64'])
    .controller('myController', [

    '$base64', '$scope', 
    function($base64, $scope) {
    
        $scope.encoded = $base64.encode('a string');
        $scope.decoded = $base64.decode('YSBzdHJpbmc=');
}]);

Và đây là cách bạn giải mã các chuỗi được mã hóa base64:

với Dojo.js

Để mã hóa một mảng byte bằng dojox.encoding.base64: 2 bình luận chia sẻ

answer

65

Một số trình duyệt như Firefox, Chrome, Safari, Opera và IE10 + có thể xử lý Base64 nguyên bản. Hãy xem câu hỏi Stackoverflow này . Nó đang sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
2và các
//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
3chức năng .

Đối với JavaScript phía máy chủ (Node), bạn có thể sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
8s để giải mã.

Nếu bạn đang tìm kiếm một giải pháp trình duyệt chéo, có các thư viện hiện có như CryptoJS hoặc mã như: 5 bình luận chia sẻ

answer

44

Một số trình duyệt như Firefox, Chrome, Safari, Opera và IE10 + có thể xử lý Base64 nguyên bản. Hãy xem câu hỏi Stackoverflow này . Nó đang sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
2và các
//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
3chức năng .

decodeBase64 = function(s) {
    var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=s.length;
    var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    for(i=0;i<64;i++){e[A.charAt(i)]=i;}
    for(x=0;x=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
    }
    return r;
};

Đối với JavaScript phía máy chủ (Node), bạn có thể sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
8s để giải mã. 5 bình luận chia sẻ

answer

35

Một số trình duyệt như Firefox, Chrome, Safari, Opera và IE10 + có thể xử lý Base64 nguyên bản. Hãy xem câu hỏi Stackoverflow này . Nó đang sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
2và các
//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
3chức năng .

function decode_base64 (s)
{
    var e = {}, i, k, v = [], r = '', w = String.fromCharCode;
    var n = [[65, 91], [97, 123], [48, 58], [43, 44], [47, 48]];

    for (z in n)
    {
        for (i = n[z][0]; i < n[z][1]; i++)
        {
            v.push(w(i));
        }
    }
    for (i = 0; i < 64; i++)
    {
        e[v[i]] = i;
    }

    for (i = 0; i < s.length; i+=72)
    {
        var b = 0, c, x, l = 0, o = s.substring(i, i+72);
        for (x = 0; x < o.length; x++)
        {
            c = e[o.charAt(x)];
            b = (b << 6) + c;
            l += 6;
            while (l >= 8)
            {
                r += w((b >>> (l -= 8)) % 256);
            }
         }
    }
    return r;
}

Đối với JavaScript phía máy chủ (Node), bạn có thể sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
8s để giải mã. 2 bình luận chia sẻ

answer

16

Một số trình duyệt như Firefox, Chrome, Safari, Opera và IE10 + có thể xử lý Base64 nguyên bản. Hãy xem câu hỏi Stackoverflow này . Nó đang sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
2và các
//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
3chức năng . 1 bình luận chia sẻ

answer

11

Một số trình duyệt như Firefox, Chrome, Safari, Opera và IE10 + có thể xử lý Base64 nguyên bản. Hãy xem câu hỏi Stackoverflow này . Nó đang sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
2và các
//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
3chức năng .

Đối với JavaScript phía máy chủ (Node), bạn có thể sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
8s để giải mã. 2 bình luận chia sẻ

answer

10

Một số trình duyệt như Firefox, Chrome, Safari, Opera và IE10 + có thể xử lý Base64 nguyên bản. Hãy xem câu hỏi Stackoverflow này . Nó đang sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
2và các
//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
3chức năng .

Đối với JavaScript phía máy chủ (Node), bạn có thể sử dụng

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
8s để giải mã.

function decode_base64(s) {
  var b=l=0, r='',
  m='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  s.split('').forEach(function (v) {
    b=(b<<6)+m.indexOf(v); l+=6;
    if (l>=8) r+=String.fromCharCode((b>>>(l-=8))&0xff);
  });
  return r;
}

Nếu bạn đang tìm kiếm một giải pháp trình duyệt chéo, có các thư viện hiện có như CryptoJS hoặc mã như:

Với cái thứ hai, bạn cần phải kiểm tra kỹ lưỡng chức năng về khả năng tương thích trên nhiều trình duyệt. Và lỗi đã được báo cáo .

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
0

234 hữu ích 4 bình luận chia sẻ

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
1

Trong khi cố gắng tìm thêm thông tin về chuỗi JavaScript dưới dạng mảng, tôi đã vấp phải mẹo chuyên nghiệp này bằng cách sử dụng

var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();
6regex để chuyển qua một chuỗi. Điều này làm giảm kích thước mã nhiều hơn bằng cách thay thế chuỗi tại chỗ và loại bỏ sự cần thiết phải giữ một biến trả về.

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
2

Tuy nhiên, nếu bạn đang tìm kiếm một cái gì đó truyền thống hơn một chút có lẽ những điều sau đây phù hợp với sở thích của bạn hơn.

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
3

Tôi không gặp phải vấn đề null cuối cùng vì vậy điều này đã được xóa để vẫn ở dưới mức nhưng nó sẽ dễ dàng được giải quyết bằng a

var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();
7hoặc a
var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();
8nếu bạn muốn, nếu điều này gây ra vấn đề cho bạn.

I E.

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
4

Ghi chú:

Kết quả là một chuỗi byte ascii, nếu bạn cần unicode thì đơn giản nhất là

var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();
9chuỗi byte mà sau đó có thể được giải mã
var str = dojox.encoding.base64.encode(myByteArray);
0để tạo ra chuỗi unicode.

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
5

var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();
9không được dùng nữa, chúng tôi có thể thay đổi chức năng của mình để hỗ trợ unicode trực tiếp mà không cần
var b = new Buffer('SmF2YVNjcmlwdA==', 'base64')
var s = b.toString();
9hoặc
var str = dojox.encoding.base64.encode(myByteArray);
3chúng tôi có thể tạo một
var str = dojox.encoding.base64.encode(myByteArray);
4chuỗi thoát sẵn sàng cho giải mã URI.

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
6

nJoy!

10 hữu ích 4 bình luận chia sẻ 4 bình luận chia sẻ

answer

6

Tôi đã thử các quy trình Javascript tại phpjs.org và chúng hoạt động tốt.

Lần đầu tiên tôi thử các quy trình được đề xuất trong câu trả lời đã chọn bởi Ranhiru Cooray - http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html

Tôi thấy rằng chúng không hoạt động trong mọi hoàn cảnh. Tôi đã viết một trường hợp thử nghiệm trong đó các quy trình này không thành công và đăng chúng lên GitHub tại:

https://github.com/scottcarter/base64_javascript_test_data.git

Mình cũng đăng comment vào bài blog ở ntt.cc để cảnh báo tác giả (đang chờ kiểm duyệt - bài đã cũ nên không chắc comment sẽ được đăng).

6 hữu ích 0 bình luận chia sẻ 0 bình luận chia sẻ

answer

6

Tôi đã thử các quy trình Javascript tại phpjs.org và chúng hoạt động tốt.

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
7

6 hữu ích 0 bình luận chia sẻ 0 bình luận chia sẻ

answer

4

Tôi đã thử các quy trình Javascript tại phpjs.org và chúng hoạt động tốt.

Lần đầu tiên tôi thử các quy trình được đề xuất trong câu trả lời đã chọn bởi Ranhiru Cooray - http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
8

Tôi thấy rằng chúng không hoạt động trong mọi hoàn cảnh. Tôi đã viết một trường hợp thử nghiệm trong đó các quy trình này không thành công và đăng chúng lên GitHub tại: 0 bình luận chia sẻ

answer

1

Tôi đã thử các quy trình Javascript tại phpjs.org và chúng hoạt động tốt.

Lần đầu tiên tôi thử các quy trình được đề xuất trong câu trả lời đã chọn bởi Ranhiru Cooray - http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html 0 bình luận chia sẻ

answer

1

Tôi đã thử các quy trình Javascript tại phpjs.org và chúng hoạt động tốt.

// Define the string
var string = 'Hello World!';

// Encode the String
var encodedString = btoa(string);
console.log(encodedString); // Outputs: "SGVsbG8gV29ybGQh"

// Decode the String
var decodedString = atob(encodedString);
console.log(decodedString); // Outputs: "Hello World!"
9

Lần đầu tiên tôi thử các quy trình được đề xuất trong câu trả lời đã chọn bởi Ranhiru Cooray - http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html 0 bình luận chia sẻ

answer

0

Tôi đã thử các quy trình Javascript tại phpjs.org và chúng hoạt động tốt.

Lần đầu tiên tôi thử các quy trình được đề xuất trong câu trả lời đã chọn bởi Ranhiru Cooray - http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
0

Tôi thấy rằng chúng không hoạt động trong mọi hoàn cảnh. Tôi đã viết một trường hợp thử nghiệm trong đó các quy trình này không thành công và đăng chúng lên GitHub tại:

Mình cũng đăng comment vào bài blog ở ntt.cc để cảnh báo tác giả (đang chờ kiểm duyệt - bài đã cũ nên không chắc comment sẽ được đăng). 0 bình luận chia sẻ

answer

0

Tôi đã thử các quy trình Javascript tại phpjs.org và chúng hoạt động tốt.Win-1251 cho các mã hóa không phải acsi hoặc iso-8859-1.

Lần đầu tiên tôi thử các quy trình được đề xuất trong câu trả lời đã chọn bởi Ranhiru Cooray - http://ntt.cc/2008/01/19/base64-encoder-decoder-with-javascript.html

Tôi thấy rằng chúng không hoạt động trong mọi hoàn cảnh. Tôi đã viết một trường hợp thử nghiệm trong đó các quy trình này không thành công và đăng chúng lên GitHub tại:

Mình cũng đăng comment vào bài blog ở ntt.cc để cảnh báo tác giả (đang chờ kiểm duyệt - bài đã cũ nên không chắc comment sẽ được đăng).

6 hữu ích 0 bình luận chia sẻ

Đối với những gì nó đáng giá, tôi đã lấy cảm hứng từ các câu trả lời khác và viết một tiện ích nhỏ gọi các API cụ thể của nền tảng để được sử dụng phổ biến từ Node.js hoặc trình duyệt:

//Buffer() requires a number, array or string as the first parameter, and an optional encoding type as the second parameter. 
// Default is utf8, possible encoding types are ascii, utf8, ucs2, base64, binary, and hex
var b = new Buffer('JavaScript');
// If we don't use toString(), JavaScript assumes we want to convert the object to utf8.
// We can make it convert to other formats by passing the encoding type to toString().
var s = b.toString('base64');
1

Mình cũng đăng comment vào bài blog ở ntt.cc để cảnh báo tác giả (đang chờ kiểm duyệt - bài đã cũ nên không chắc comment sẽ được đăng). 0 bình luận chia sẻ