C++string,char* 字符数组,int类型之间的转换
string、int 常见类型之间相互转换
int & string 之间的转换
- C++中更多的是使用流对象来实现类型转换
针对流对象 sstream实现
int,float 类型都可以实现
#include <sstream>
//int convert to string
void int2str(const int &int_temp,string &string_temp){
stringstream s_stream;
s_stream<<int_temp;
string_temp=s_stream.str();
//s_stream>>string_temp; // 也可以实现
}
//string convert to int
void str2int(const string &string_temp,int &int_temp){
stringstream s_stream(string_temp);
s_stream>>int_temp;
}
- 其他的方法
c_str()函数
string.c_str() 可以将string字符串转换成一个指向与string相同字符数组的头指针
// atoi
void str2int(string &string_temp,int &int_temp){
int_temp=atoi(string_temp.c_str());
}
// stoi实现
void str2int_stoi_version(string& string_temp,int &int_temp){
int_temp=stoi(string_temp);
}
字符数组char* 与string之间的转换
- 字符数组转为string
char ch [] = "ABCDEFGHIJKL";
string str(ch);//也可string str = ch;
// other way
char ch [] = "ABCDEFGHIJKL";
string str;
str = ch;//在原有基础上添加可以用str += ch;
- string转为字符数组
char buf[8];
string str("ABCDEFG");
length = str.copy(buf,8); //str.copy() return number of character copied
buf[length] = '\0'; //末尾置0
char buf[8];
string str("ABCDEFG");
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 8);
strcpy()函数
//char* strcpy( char* dest, const char* src );
#include <iostream>
#include <cstring>
#include <memory>
int main()
{
const char* src = "Take the test.";
// src[0] = 'M'; // can't modify string literal
auto dst = std::make_unique<char[]>(std::strlen(src)+1); // +1 for the null terminator
std::strcpy(dst.get(), src);
dst[0] = 'M';
std::cout << src << '\n' << dst.get() << '\n';
}
// Take the test.
// Make the test.
strncpy()函数
// char *strncpy( char *dest, const char *src, std::size_t count );
#include <iostream>
#include <cstring>
int main()
{
const char* src = "hi";
char dest[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
std::strncpy(dest, src, 5);
std::cout << "The contents of dest are: ";
for (char c : dest) {
if (c) {
std::cout << c << ' ';
} else {
std::cout << "\\0" << ' ';
}
}
std::cout << '\n';
}
//The contents of dest are: h i \0 \0 \0 f
int 和 char
int a=1;
char c=a+'0'; //c的值就是'1'的ASCII码值
C++string,char* 字符数组,int类型之间的转换的更多相关文章
- char类型和int类型之间的转换
在视屏课程第二章里,我们已经学习了一些常用的数据类型转换.然而,有一些时候我们会经常会遇到将char类型转换成int类型,或者需要将int类型转换为char类型的情况. 这里,我们来探讨一下这种不常用 ...
- 03.枚举和string以及int类型之间的转换
练习1: 将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...
- java中字符数组与字符串之间互相转换的方法
public static void main(String[] args) { //1.字符数组 转换成 字符串 //(1)直接在构造String时转换 char[] array = new cha ...
- string类型和int类型之间的转换
一.string转int 1. 使用string流 /* 字符串转整型 */ /* * istringstream:从 string 读取数据 * ostringstream:向 string 写入数 ...
- Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换
public class DigitalTrans { /** * 数字字符串转ASCII码字符串 * * @param String * 字符串 * @return ASCII字符串 */ publ ...
- c#中RGB与int类型之间的转换
Color color = Color.FromArgb(0, 0, 255);int colorInt = ParseRGB(color); --------------------- int Pa ...
- NSString / NSData / char* 类型之间的转换
转自网络: NSString / NSData / char* 类型之间的转换 1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; ch ...
- DB2中字符、数字和日期类型之间的转换
DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别 ...
- int([x[, base]]) : 将一个字符转换为int类型,base表示进制
int([x[, base]]) : 将一个字符转换为int类型,base表示进制 >>> int(-12) -12 >>> int(-12.00) -12 > ...
随机推荐
- Active Directory域
引言 在 Microsoft® Windows® 2000 Server 操作系统的诸多增强功能中,Microsoft Active Directory™ 功能的引入意义最为重大,但也最常引起困惑.与 ...
- win10应用商店卸载后重装教程
方法一 先进这个链接 http://go.microsoft.com/fwlink/?LinkId=619547 下载一个记事本文件,并且把它保存到你的“下载” 里面. 管理员身份打开Power ...
- 林大妈的JavaScript基础知识(三):JavaScript编程(4)数组
数组,是一段线性分配的,具有非常高性能的数据结构.简单地说,数组以连续的空间存储,通过整数地计算偏移量访问其中的元素,将读取修改的时间复杂度降低至O(1),我们称之为猝发式存取.是不是非常期待?没错, ...
- Zookeeper 学习笔记(一)之功能介绍
Zookeeper 主要在以下场景中可以使用 一,命名服务(用到了zookeeper的文件系统) 命名服务是指通过指定的名字来获取资源或者服务的地址,利用zk创建一个全局的路径,提供服务的地址或者一个 ...
- Java 设置PDF文档浏览偏好
在查看PDF文档时,可进行一些浏览偏好设置,例如是否全屏浏览.隐藏或显示菜单栏/工具栏.设置页面布局模式等,下面将通过Java编程的方式来演示如何设置. 使用工具: Free Spire.PDF fo ...
- 前端笔记之微信小程序(三)GET请求案例&文件上传和相册API&配置https
一.信息流小程序-GET请求案例 1.1服务端接口开发 一定要养成接口的意识,前端单打独斗出不来任何效果,必须有接口配合,写一个带有分页.关键词查询的接口: 分页接口:http://127.0.0.1 ...
- 直方图均衡基本原理及Python实现
1. 基本原理 通过一个变换,将输入图像的灰度级转换为`均匀分布`,变换后的灰度级的概率密度函数为 $$P_s(s) = \frac{1}{L-1}$$ 直方图均衡的变换为 $$s = T(r) = ...
- Java中的魔法类-Unsafe
Unsafe是位于sun.misc包下的一个类,主要提供一些用于执行低级别.不安全操作的方法,如直接访问系统内存资源.自主管理内存资源等,这些方法在提升Java运行效率.增强Java语言底层资源操作能 ...
- Amazon S3
Amazon S3 是什么? Amazon S3 是亚马逊推出的一款存储服务,名为 Amazon Simple Storage Service,即亚马逊简单存储服务. 有些 S3 的概念需要了解一下: ...
- android ——调用摄像头拍照和相册
先在布局文件中加入两个按钮和一个图片控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...