How can I decode a base64 string in Javascript? • GITNUX | 您所在的位置:网站首页 › 魔兽怀旧法师餐桌 › How can I decode a base64 string in Javascript? • GITNUX |
Decoding a base64 string in JavaScript is easy with the `atob()` function. This function takes a base64 encoded string as input and returns the decoded string. However, this function is not supported in Node.js; instead, you can use the `Buffer` class to decode it there. In this post we will look at how to do both of these tasks using examples for each case. Programming GuideYou can decode a base64 string in JavaScript using the `atob()` function. Here’s an example: // Your base64 encoded string var base64String = "SGVsbG8sIFdvcmxkIQ=="; // Decode the base64 string var decodedString = atob(base64String); console.log(decodedString); // Outputs: "Hello, World."`atob()` takes a base64 encoded string as input and returns the decoded string. Note that this function is not supported in Node.js, only in browsers. If you need to do this in Node.js, you can use the `Buffer` class: // Your base64 encoded string var base64String = "SGVsbG8sIFdvcmxkIQ=="; // Decode the base64 string in Node.js var decodedString = Buffer.from(base64String, 'base64').toString('utf-8'); console.log(decodedString); // Outputs: "Hello, World."In this case, `Buffer.from()` creates a new Buffer from the base64 encoded string, and `toString(‘utf-8’)` converts the Buffer to a readable UTF-8 string. ConclusionThe `atob()` function can be used to decode a base64 encoded string in JavaScript. It is not supported in Node.js, but the `Buffer` class can be used instead for decoding strings in this environment. |
CopyRight 2018-2019 实验室设备网 版权所有 |