base64编码

base64编码:
//
// Created by Harold on 2022/8/14/0014.
//
#include <iostream>
#include <string>
#include <vector>
#define MAX_SIZE 1000
using namespace std;
class Solution{
public:
    void getInput(){
        cin>>codeList;
    }
    void getTheAns(){
        int startIndex = 0, endIndex;
        int codeLen = codeList.size();
        if(codeLen <= 0){
            cout<<"";
        }
        while(startIndex < codeLen){
            if(startIndex + 2 < codeLen){
                endIndex = startIndex + 2;
            }
            else{
                endIndex = codeLen - 1;
            }
            decode(codeList.substr(startIndex,endIndex+1));
            startIndex = endIndex + 1;
        }
        cout<<base64StrList;
    }
    void run(){
        getInput();
        getTheAns();
    }
    void decode(string subCode){
        vector<int> base64(4,0);
        int subCodeLen = subCode.size();
        string res(4,'0');
        base64[0] = int(subCode[0]) >> 2; // 取第一个字节前6位(通过右移两位)
        if(subCodeLen == 1){
            base64[1] = ((int(subCode[0]) & 0x03) << 4);
            base64[2] = MAX_SIZE;
            base64[3] = MAX_SIZE;
        }
        else if(subCodeLen == 2){
            base64[1] = ((int(subCode[0]) & 0x03) << 4) + (int(subCode[1]) >> 4);
            base64[2] = ((int(subCode[1]) & 0x0f) << 2);
            base64[3] = MAX_SIZE;
        }
        else if(subCodeLen == 3){
            base64[1] = ((int(subCode[0]) & 0x03) << 4) + (int(subCode[1]) >> 4); // 取第一个字节后2位作为高位,第二个字节前4位作为低位
            base64[2] = ((int(subCode[1]) & 0x0f) << 2) + (int(subCode[2]) >> 6);
            base64[3] = (int(subCode[2]) & 0x3f);
        }
        else{
            cout<<"error !\n";
            return;
        }

        for(int i = 0; i < base64.size(); i++){
            if(0 <= base64[i] && base64[i] <= 25){
                res[i] = char('A' + base64[i]);
            }
            else if(26 <= base64[i] && base64[i] <= 51){
                res[i] = char('a' + base64[i] - 26);
            }
            else if(52 <= base64[i] && base64[i] <=61){
                res[i] = char('0' + base64[i] - 52);
            }
            else if(base64[i] == 62){
                res[i] = '+';
            }
            else if(base64[i] == 63){
                res[i] = '/';
            }
            else if(base64[i] == MAX_SIZE){
                res[i] = '=';
            }
            else{
                cout<<"error !\n";
                return;
            }
        }
        base64StrList += res;
    }

private:
    string codeList;
    string base64StrList;
};
int main(){
    Solution s;
    s.run();
}



#大疆笔试##算法题#
全部评论
事后诸葛亮
1 回复 分享
发布于 2022-08-14 22:13

相关推荐

2024-12-27 23:45
已编辑
三江学院 Java
程序员牛肉:死局。学历+无实习+项目比较简单一点。基本就代表失业了。 尤其是项目,功能点实在是太假了。而且提问点也很少。第一个项目中的使用jwt和threadlocal也可以作为亮点写出来嘛?第二个项目中的“后端使用restful风格”,“前端采用vue.JS”,“使用redis”也可以作为亮点嘛? 项目实在是太简单了,基本就是1+1=2的水平。而你目标投递的肯定也是小厂,可小厂哪里有什么培养制度,由于成本的问题,人家更希望你来能直接干活,所以你投小厂也很难投。基本就是死局,也不一定非要走后端这条路。可以再学一学后端之后走测试或者前端。 除此之外,不要相信任何付费改简历的。你这份简历没有改的必要了,先沉淀沉淀
点赞 评论 收藏
分享
02-11 12:20
门头沟学院 Java
面试中的青提很胆小:我不信有比我们学校更逆天的,计算机专业就业第一位是我们学校二餐厅的打印店
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务