本文介绍了如何将整数值转换为char数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为VC ++的新手,



我想确认最大整数变量可以转换为char sizeof 6.



例如,

As a novice of VC++,

I want to confirm that maximum of integer variable can convert to char sizeof 6.

For example,

int x;

CString s = L"";

char    CHAR6BYTES[6];

s.Format(_T("%06d"), x);

::ZeroMemory(CHAR6BYTES,6);

WideCharToMultiByte(CP_ACP, s.GetBuffer(), s.GetLength(), CHAR6BYTES, 6, NULL, NULL);



这是将整数变量转换为char 6大小变量的最佳方法吗?

或谁知道更好的方法...



提前谢谢。



我尝试过:



寻找2小时网络搜索...


Is this best ways to convert integer variable to char 6 size variable?
Or who knows the better ways...

Thank you in advance.

What I have tried:

Finding 2 hours web searching ...

推荐答案

char sNumber[7];
sprintf_s(sNumber, 7, "%06d", x);


  1. 为什么你想将输出限制为六个字符(而你知道, int 可以更大一些)?
  2. 你为什么试图代表一个未初始化的值( x )?
  3. 为什么首先代表 int 带有宽字符串的值然后将其转换为ANSI 1?
  1. Why do you want to limit the output to six characters (while, you know, int can be much more bigger)?
  2. Why are you trying to represent a uninitialized value (x)?
  3. Why are you first representing the int value with a wide string and then converting it to ANSI one?





Richard 代码,非常仔细地解决第3点,显示了要走的路。但是,如果删除六个字符约束,您还可以使用以下方法:



Richard code, addressing very carefully point 3, shows the way to go. However, if you remove the six characters constraint, you could also use the following approach:

#include<iostream>
#include <sstream>
using namespace std;

int main()
{
  int i = 12345;

  string s;
  ostringstream iss;
  iss << i;
  s = iss.str();
  cout << "the string representation of the number is " << s << endl;
}



这篇关于如何将整数值转换为char数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:18
查看更多