int 怎么转成char数组?

jesse1117 2014-07-14 04:21:04
char数组大小为8 前补0 比如
123 变成{‘0’,‘0’,‘0’,‘0’,‘0’,‘1’,‘2’,‘3’}
...全文
976 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
cddchina 2014-07-25
  • 打赏
  • 举报
回复
1. sprintf(); 格式化字符串; 2.可以用itoa()函数转化; 3.在MFC中还可以用Format
Napoleon_Aiert 2014-07-16
  • 打赏
  • 举报
回复
引用 20 楼 jesse1117 的回复:
[quote=引用 11 楼 xmnathan 的回复:] 一个小问题,好多大神回答
。。。 我都木有想到 刚问完都觉得问题太二比了。。 40分就这么没了 没想到 还知道了这么多方法[/quote] 呵呵,我是来赚分的,帮顶楼主,不过楼主这问题实在不用大费周章。
l4kangaxx 2014-07-16
  • 打赏
  • 举报
回复
引用 25 楼 zhao4zhong1 的回复:
[quote=引用 23 楼 lion_kangaxx 的回复:] [quote=引用 13 楼 zhao4zhong1 的回复:] char ch[9]; ch[8]='A'; sprintf(ch, "%.8d", 12345678); printf("ch[8]==%d\n",ch[8]);//运行看看输出ch[8]==65还是输出ch[8]==0
多加一位的目的是什么?不是8位数么?我一般这么写
    char ch[8];

    sprintf(ch, "%.8d", 12345678);
    for (int i = 0;i<8;i++)
        printf("ch[%d]==%d\n",i,ch[i]);
[/quote] sprintf输出8个数字时,还要在最后添加一个'\0'[/quote] 原来如此,这函数也保持着整体风格,内存不保护。多谢大神。
twtiqfn 2014-07-15
  • 打赏
  • 举报
回复
这应该不难吧
jesse1117 2014-07-15
  • 打赏
  • 举报
回复
引用 11 楼 xmnathan 的回复:
一个小问题,好多大神回答
。。。 我都木有想到 刚问完都觉得问题太二比了。。 40分就这么没了 没想到 还知道了这么多方法
我看你有戏 2014-07-15
  • 打赏
  • 举报
回复
sprintf
xiaobiao77 2014-07-15
  • 打赏
  • 举报
回复
把最后三个赋值1 2 3就行了 很简单的
赵4老师 2014-07-15
  • 打赏
  • 举报
回复
引用 23 楼 lion_kangaxx 的回复:
[quote=引用 13 楼 zhao4zhong1 的回复:] char ch[9]; ch[8]='A'; sprintf(ch, "%.8d", 12345678); printf("ch[8]==%d\n",ch[8]);//运行看看输出ch[8]==65还是输出ch[8]==0
多加一位的目的是什么?不是8位数么?我一般这么写
    char ch[8];

    sprintf(ch, "%.8d", 12345678);
    for (int i = 0;i<8;i++)
        printf("ch[%d]==%d\n",i,ch[i]);
[/quote] sprintf输出8个数字时,还要在最后添加一个'\0'
l4kangaxx 2014-07-15
  • 打赏
  • 举报
回复
c/c++数组难道不是从0开始的?我pascal写的多,大牛不要忽悠我
l4kangaxx 2014-07-15
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
char ch[9]; ch[8]='A'; sprintf(ch, "%.8d", 12345678); printf("ch[8]==%d\n",ch[8]);//运行看看输出ch[8]==65还是输出ch[8]==0
多加一位的目的是什么?不是8位数么?我一般这么写
    char ch[8];

    sprintf(ch, "%.8d", 12345678);
    for (int i = 0;i<8;i++)
        printf("ch[%d]==%d\n",i,ch[i]);
yizhiluoye 2014-07-15
  • 打赏
  • 举报
回复
只能分开转换成Char
mujiok2003 2014-07-14
  • 打赏
  • 举报
回复
引用 17 楼 virtualxmars 的回复:
[quote=引用 16 楼 mujiok2003 的回复:] 既然是C++, 使用std::stringstream.
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

int main() {
   std::ostringstream oss;
   oss << std::setw(8) << std::setfill('0');
   
   oss << 123;
   std::cout << oss.str() << std::endl; //00000123
	
   return 0;
}
楼主要求的转换结果是 char[8]的数组嘛,用stringstream或者sprintf转换出来的都是带'\0'结束符的,除非经过转换,否则不满足题目要求,不过倒是可以利用一下:

// 数组总共8位
const int CountDigits = 8;

void convert(int val, char* digits) {
    char digitsWithNull[9];
    sprintf(digitsWithNull, "%08d", val);
    memcpy(digits, digitsWithNull, CountDigits);
}
[/quote] 我在C++程序中很少用数组(除了要跟别的api配合), 直接使用std::vector/std::string/std:array
virtualxmars 2014-07-14
  • 打赏
  • 举报
回复
引用 16 楼 mujiok2003 的回复:
既然是C++, 使用std::stringstream.
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

int main() {
   std::ostringstream oss;
   oss << std::setw(8) << std::setfill('0');
   
   oss << 123;
   std::cout << oss.str() << std::endl; //00000123
	
   return 0;
}
楼主要求的转换结果是 char[8]的数组嘛,用stringstream或者sprintf转换出来的都是带'\0'结束符的,除非经过转换,否则不满足题目要求,不过倒是可以利用一下:

// 数组总共8位
const int CountDigits = 8;

void convert(int val, char* digits) {
    char digitsWithNull[9];
    sprintf(digitsWithNull, "%08d", val);
    memcpy(digits, digitsWithNull, CountDigits);
}
mujiok2003 2014-07-14
  • 打赏
  • 举报
回复
既然是C++, 使用std::stringstream.
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;

int main() {
   std::ostringstream oss;
   oss << std::setw(8) << std::setfill('0');
   
   oss << 123;
   std::cout << oss.str() << std::endl; //00000123
	
   return 0;
}
virtualxmars 2014-07-14
  • 打赏
  • 举报
回复
请笑纳

#include <cstdio>
#include <cstring>

// 数组总共8位
const int CountDigits = 8;

void convert(int val, char* digits) {
    // 首先全部初始化为字符'0',方便后面的处理,例如:如果数字是val是1,那digits的前7位就不用设置了
    memset(digits, '0', CountDigits);

    // 由于转换结果是左补'0',所以应该从数的右边开始进行转换。相应的,存储的时候,也应该从数组的最右边开始存储
    // 数组最右边的字符地址,应该是起始地址 + (字符总数 - 1),因为数字的索引是以0为基数的
    digits = digits+CountDigits-1;

    // 当数字最后被除为0时,就可以停止处理了,因为函数最开始时,已经将所有字符默认设置为'0'
    while(val!=0) {
        // 字符'0' 加上 数字0~9,对应的就是字符'0' ~ '9'
        // 而对于一个较大的数,执行模10操作(C++对应 % 操作符),得到的就是该数的个位数
        *digits += (val % 10);

        // 字符存放位置左移一位,以便存更高位数的数值字符
        digits--;

        // 待转换的数除10, 这样下次进行模10操作时,得到的就是高位数的结果了
        val /= 10;
    }
}

int main() {
    // 这里定义了9个字符,而不是8个。并将最后一个自动设置为终结符'\0'
    // convert函数只会影响前8个字符,这样处理,最后就可以了通过printf来输出了
    char digits[9] = {0};
    convert(12345678, digits);
    printf("Converted:%s\n", digits);

    return 0;
}
lin5161678 2014-07-14
  • 打赏
  • 举报
回复
引用 13 楼 zhao4zhong1 的回复:
char ch[9]; ch[8]='A'; sprintf(ch, "%.8d", 12345678); printf("ch[8]==%d\n",ch[8]);//运行看看输出ch[8]==65还是输出ch[8]==0
多谢 提醒 的确是我错了 进修中
赵4老师 2014-07-14
  • 打赏
  • 举报
回复
char ch[9]; ch[8]='A'; sprintf(ch, "%.8d", 12345678); printf("ch[8]==%d\n",ch[8]);//运行看看输出ch[8]==65还是输出ch[8]==0
赵4老师 2014-07-14
  • 打赏
  • 举报
回复
引用 7 楼 lin5161678 的回复:
[quote=引用 6 楼 zhao4zhong1 的回复:] [quote=引用 4 楼 lin5161678 的回复:]
#include <stdio.h>

int main(void) {
	int n = 123;
	char ch[8];
	sprintf(ch, "%.8d", n);
	for(int i=0; i<8; ++i)
		putchar(ch[i]);
	return 0;
}
char ch[8+1]; [/quote]+1是完全多余的 这不是字符串[/quote] char ch[9]; ch[8]='A'; sprintf(ch, "%.8d", 1); printf("ch[8]==%d\n",ch[8]);//运行看看输出ch[8]==65还是输出ch[8]==0
  • 打赏
  • 举报
回复
一个小问题,好多大神回答
lin5161678 2014-07-14
  • 打赏
  • 举报
回复
引用 9 楼 zgszlhtao 的回复:
printf( "%s\n", nArray ); }
错了
加载更多回复(9)

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧