转换将int ASCII字符 [英] Convert an int to ASCII character

查看:360
本文介绍了转换将int ASCII字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int i = 6;

和我想

char c = '6'

进行转换。任何简单的方法来建议?

by conversion. Any simple way to suggest?

编辑:
此外,我需要生成一个随机数,并转换为一个字符,然后添加一个.TXT和访问它在ifstream的。

also i need to generate a random number, and convert to a char, then add a '.txt' and access it in an ifstream.

推荐答案

直接的方式:

char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char aChar = digits[i];

更安全的方式:

char aChar = '0' + i;

通用方式:

itoa(i, ...)

方便的方法:

sprintf(myString, "%d", i)

C ++的方式:的(从Dave18回答)

C++ way: (taken from Dave18 answer)

std::ostringstream oss;
oss << 6;

老大方式:

乔,给我写一个int到字符转换

Joe, write me an int to char converter

SO方式:

大家好,我怎么避免读
  新手指南C ++?

Guys, how do I avoid reading beginner's guide to C++?

注释:我已经添加了便捷的方式和C ++的方式(有一个完整的集合),我保存这是一个维基。

Comment: I've added Handy way and C++ way (to have a complete collection) and I'm saving this as a wiki.

这篇关于转换将int ASCII字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆