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码值

原文地址:https://www.cnblogs.com/GeekDanny/p/10992932.html

时间: 2024-08-14 20:35:32

C++string,char* 字符数组,int类型之间的转换的相关文章

Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换

public class DigitalTrans { /** * 数字字符串转ASCII码字符串 * * @param String * 字符串 * @return ASCII字符串 */ public static String StringToAsciiString(String content) { String result = ""; int max = content.length(); for (int i = 0; i < max; i++) { char c

NSString / NSData / char* 类型之间的转换

NSString / NSData / char* 类型之间的转换 1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length])); 与strcpy相比,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节 2. NS

DB2中字符、数字和日期类型之间的转换

DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别是如何实现的呢?在Oracle这几个类型之间的转换是十分方便的,通过 to_char|to_date|to_number函数即可完成类型转换.本小节主要介绍DB2中的一些知识,从Oracle转过来的DBA或开发人 员,可以对比着学习. 数字型到字符型的转换 DB2中的col_a字段 字段类型 到字符类型的转换

stringstream的用法-类型之间的转换

本文来自: http://www.builder.com.cn/2003/0304/83250.shtmlhttp://www.cppblog.com/alantop/archive/2007/07/10/27823.html 使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中,我将展示怎样使用这些库来实现安全和自动的类型转换. 为什么要学习 如果你已习惯了&

【opencv基础】opencv和dlib库中rectangle类型之间的转换

前言 最近使用dlib库的同时也会用到opencv,特别是由于对dlib库的画图函数不熟悉,都想着转换到opencv进行show.本文介绍一下两种开源库中rectangle类型之间的转换. 类型说明 opencv中cv::Rect    以及opencv中的rectangle函数: void cv::rectangle( InputOutputArray img, Point pt1, Point pt2, const Scalar & color, int thickness = 1, int

pytorch--基础类型之间的转换

在pytorch自己定义张量并进行计算的时候,往往会因为类型不匹配而报错,这里稍微记下pytorch之间的类型转换: 对tensor基础类型进行转换:比如说int().float().long().double().byte()等,直接.类型即可,例如float()->int:data.int() GPU与CPU类型之间的转换:GPU->CPU:data.cpu()CPU->GPU:data.cuda() Variable与Tensor:貌似Variable已经被合并到Tensor中了:

Swift数字类型之间的转换

Swift数字类型之间的转换Swift是一种安全的语言,对于类型的检查非常严格,不同类型之间不能随便转换.一.整型之间的转换在C和Objective-C等其他语言中,整型之间有两种转换方法:从小范围数到大范围数转换是自动的:从大范围数到小范围数需要强制类型转换,有可能造成数据精度的丢失.而在Swift中这两种方法是行不通的,我们需要通过一些函数进行显式地转换,代码如下: let historyScore:UInt8 = 90 let englishScore:UInt16 = 130 let t

Java数据类型中String、Integer、int相互间的转换

1.Integer转换成int的方法 Integer i;  int k = i.intValue(); 即Integer.intValue(); 2.int转换成Integer int i; Integer it = new Integer(i); 3.String转换成int的方法 String str = "10";   Integer it = new Interger(str); int i = it.intValue(); 即:int i = Integer.intValu

String,Integer,int类型之间的相互转换

String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new Integer(int a); //封装的基本原理 Integer it2 = Integer.valueOf(int a); int -> String String s2=10+""; 2.  包装数据类型到基本数据类型的转换 String -> int int i4=Int