c++ string 与 char 互转

很简单如下

    char bts[] = {'A','B','C','D','E'};
printf("%s\n",bts);
//char to string
std::string strBts = bts;
std::cout << strBts << std::endl; //string to char
char *theBts = (char *)strBts.c_str();
printf("%s\n",theBts);

c++ base64 工具

//
// base64.h
// CPPWork
// from http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
// Created by cocoa on 16/8/5.
// Copyright © 2016年 cc. All rights reserved.
// #ifndef base64_h
#define base64_h #include <string> std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len);
std::string base64_decode(std::string const& encoded_string); #endif /* base64_h */
//
// base64.cpp
// CPPWork
//
// Created by cocoa on 16/8/5.
// Copyright © 2016年 cc. All rights reserved.
// #include "base64.h"
#include <iostream> static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/"; static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
} std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret;
int i = ;
int j = ;
unsigned char char_array_3[];
unsigned char char_array_4[]; while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++);
if (i == ) {
char_array_4[] = (char_array_3[] & 0xfc) >> ;
char_array_4[] = ((char_array_3[] & 0x03) << ) + ((char_array_3[] & 0xf0) >> );
char_array_4[] = ((char_array_3[] & 0x0f) << ) + ((char_array_3[] & 0xc0) >> );
char_array_4[] = char_array_3[] & 0x3f; for(i = ; (i <) ; i++)
ret += base64_chars[char_array_4[i]];
i = ;
}
} if (i)
{
for(j = i; j < ; j++)
char_array_3[j] = '\0'; char_array_4[] = (char_array_3[] & 0xfc) >> ;
char_array_4[] = ((char_array_3[] & 0x03) << ) + ((char_array_3[] & 0xf0) >> );
char_array_4[] = ((char_array_3[] & 0x0f) << ) + ((char_array_3[] & 0xc0) >> );
char_array_4[] = char_array_3[] & 0x3f; for (j = ; (j < i + ); j++)
ret += base64_chars[char_array_4[j]]; while((i++ < ))
ret += '='; } return ret; }
std::string base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size();
int i = ;
int j = ;
int in_ = ;
unsigned char char_array_4[], char_array_3[];
std::string ret; while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==) {
for (i = ; i <; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]); char_array_3[] = (char_array_4[] << ) + ((char_array_4[] & 0x30) >> );
char_array_3[] = ((char_array_4[] & 0xf) << ) + ((char_array_4[] & 0x3c) >> );
char_array_3[] = ((char_array_4[] & 0x3) << ) + char_array_4[]; for (i = ; (i < ); i++)
ret += char_array_3[i];
i = ;
}
} if (i) {
for (j = i; j <; j++)
char_array_4[j] = ; for (j = ; j <; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]); char_array_3[] = (char_array_4[] << ) + ((char_array_4[] & 0x30) >> );
char_array_3[] = ((char_array_4[] & 0xf) << ) + ((char_array_4[] & 0x3c) >> );
char_array_3[] = ((char_array_4[] & 0x3) << ) + char_array_4[]; for (j = ; (j < i - ); j++) ret += char_array_3[j];
} return ret;
}

c++ string 与 char 互转 以及base64的更多相关文章

  1. 2、CString与string借助char *互转

    CString是MFC中的类,MFC前端界面中获得的字符串是CString类.标准C/C++库函数是不能直接对CString类型进行操作的. string是C++中的类. 安全性 CString &g ...

  2. C#string与char互转

    string s = "asdf"; //字符转char char[] c = s.ToCharArray(); Console.WriteLine(s[]); //char转st ...

  3. PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组转换

    PChar,PAnsiChar,String,AnsiString,Char数组,AnsiChar数组之间的转换关系见下图 通过转换链,可以实现任意两个类型之间的互转.如PChar转PAnsiChar ...

  4. CString与char *互转总结

    1 前言 今天在网上看论坛,发现大家对CString与Char *互转各说一词,其实我发现提问者所说的情况与回答问题的人完全不是同一情况,这里做一总结. 首先大家得清楚一件事,一般在网上提出问题的人大 ...

  5. string与wstring互转

    string与wstring互转  C++ Code  123456789101112131415161718192021222324252627282930313233343536373839404 ...

  6. C++ TCHAR* 与char* 互转

    C++ TCHAR* 与char* 互转 在MSDN中有这么一段: Note: The ANSI code pages can be different on different computers, ...

  7. QString, string, int, char* 之间相互转换

    这三种数据类型在实际运用中经常需要互相转换,那么这里小结下它们之间的转换方法: - Qstring & string Qt中封装的类十分强大,其成员函数数量之多比STD有过之而无不及,许多程序 ...

  8. string to char* and char* to string 玩转 String 和 Char*

    char 类型是c语言中常见的一个数据类型,string是c++中的一个,它的定义为 Strings are objects that represent sequences of character ...

  9. string,const char*,char*之间的相互转换

    1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...

随机推荐

  1. AIM Tech Round 3 (Div. 2) B

    Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...

  2. django学习日志之自定义用户扩展

    django 为我们提供了强大的用户认证系统,并且提供了基于该系统的User模型,所以,很多时候,我们有必要对自己的user进行业务扩展,得到满足我们自己业务需求的user.借此,写下自己的感悟. u ...

  3. white的配置使用

    初次使用White来自动化测试10个9相加1.新建Visual C#->测试->单元测试项目2.在资源视图->引用,右键,添加引用,添加White的两个.dll文件3.在工程中添加命 ...

  4. IIS7多域名绑定同一物理目录,设置不同默认文档的解决方案

    转载自 http://zzstudy.offcn.com/archives/6159 如何解决IIS7多域名绑定同一物理目录,设置不同的默认文档的问题? 因为在一个物理目录下只有一个web.confi ...

  5. Android-activity-intent

    package com.hanqi.myapplication; import android.content.ComponentName; import android.content.Intent ...

  6. DIV的表单布局

    表单布局其实用表格最好了,可是表格的话,无法定位,这个是一个硬伤. <!DOCTYPE html> <html> <head> <meta charset=& ...

  7. 将NuGet配置到环境变量中

    https://docs.nuget.org/consume/command-line-reference Installing The NuGet command line may be insta ...

  8. Lambda表达式之Python

    一.lambda函数 1.lambda函数基础: lambda函数也叫匿名函数,即,函数没有具体的名称,而用def创建的方法是有名称的.如下: """命名的foo函数&q ...

  9. div相对浏览器移动

    <%    String path = request.getContextPath();%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  10. FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)

    FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术) Time Limit: 1000MS   Memory Limit: 257792K [Description ...