如何将整数值转换为char数据类型? [英] How do I convert integer value to char datatype?

查看:147
本文介绍了如何将整数值转换为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 ...

推荐答案

这是一种方法。但是为什么要创建Unicode字符串然后转换为ASCII。在第一个实例中用ASCII创建,例如:

That is one way of doing it. But why are you creating a Unicode string and then converting to ASCII. Create in ASCII in the first instance, something like:
char sNumber[7];
sprintf_s(sNumber, 7, "%06d", x);


您的代码提出了很多问题。

Your code rises many questions.
  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;
}


我不用C ++编程,除非必须这样做。因此,当我遇到困难时,谷歌搜索是我第一次去,因为我知道我并不是唯一一个遇到过我遇到的问题的人。快速搜索每次都证明了这一点。



因此,互联网在使用时可以成为强大的帮助工具,最好的朋友和导师。此搜索链接为您提供了大量解决方案: c ++ convert int to char [ ^ ]
I don't program in C++, not unless I have to. So when I get stuck, Google Search is my first go to as I know that I am not the only one who has encountered the problems that I experience. A quick search proves that every time.

So, the internet can be a powerful help tool, your best friend, and your mentor, when used. This search link has tonnes of solutions for you: c++ convert int to char[^]


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

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