如何在 C++ 中将 char* 转换为 unsigned short
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3103007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert char* to unsigned short in C++
提问by Elpezmuerto
I have a char* name
which is a string representation of the short I want, such as "15" and need to output this as unsigned short unitId
to a binary file. This cast must also be cross-platform compatible.
我有一个char* name
它是我想要的短的字符串表示形式,例如“15”,并且需要将其输出为unsigned short unitId
二进制文件。此演员表还必须是跨平台兼容的。
Is this the correct cast: unitId = unsigned short(temp);
这是正确的演员阵容: unitId = unsigned short(temp);
Please note that I am at an beginner level in understanding binary.
请注意,我在理解二进制方面处于初级水平。
回答by JSB????
I assume that your char* name
contains a stringrepresentation of the short that you want, i.e. "15"
.
我假设您char* name
包含您想要的短的字符串表示形式,即"15"
.
Do notcast a char*
directly to a non-pointer type. Casts in C don't actually change the data at all (with a few exceptions)--they just inform the compiler that you want to treat one type into another type. If you cast a char*
to an unsigned short
, you'll be taking the value of the pointer(which has nothing to do with the contents), chopping off everything that doesn't fit into a short
, and then throwing away the rest. This is absolutely not what you want.
千万不能一投char*
直接到非指针类型。C 中的强制转换实际上根本不更改数据(有一些例外)——它们只是通知编译器您想将一种类型处理为另一种类型。如果您将 a 转换char*
为 an unsigned short
,您将获取指针的值(与内容无关),切掉不适合 a 的所有内容short
,然后丢弃其余部分。这绝对不是你想要的。
Instead use the std::strtoul
function, which parses a string and gives you back the equivalent number:
而是使用std::strtoul
解析字符串并返回等效数字的函数:
unsigned short number = (unsigned short) strtoul(name, NULL, 0);
(You still need to use a cast, because strtoul
returns an unsigned long
. This cast is between two different integer types, however, and so is valid. The worst that can happen is that the number inside name
is too big to fit into a short
--a situation that you can check for elsewhere.)
(您仍然需要使用强制转换,因为strtoul
返回一个unsigned long
。但是,此强制转换介于两种不同的整数类型之间,因此是有效的。可能发生的最坏情况是里面的数字name
太大而无法适应short
--a 情况您可以在其他地方检查。)
回答by fredoverflow
#include <boost/lexical_cast.hpp>
unitId = boost::lexical_cast<unsigned short>(temp);
回答by Amardeep AC9MF
To convert a string to binary in C++ you can use stringstream.
要将字符串转换为 C++ 中的二进制,您可以使用 stringstream。
#include <sstream>
. . .
int somefunction()
{
unsigned short num;
char *name = "123";
std::stringstream ss(name);
ss >> num;
if (ss.fail() == false)
{
// You can write out the binary value of num. Since you mention
// cross platform in your question, be sure to enforce a byte order.
}
}
回答by Chris Card
that cast will give you (a truncated) integer version of the pointer, assuming temp is also a char*. This is almost certainly not what you want (and the syntax is wrong too). Take a look at the function atoi, it may be what you need, e.g. unitId = (unsigned short)(atoi(temp)); Note that this assumes that (a) temp is pointing to a string of digits and (b) the digits represent a number that can fit into an unsigned short
该转换将为您提供(截断的)整数版本的指针,假设 temp 也是一个 char*。这几乎肯定不是你想要的(而且语法也是错误的)。看看函数atoi,它可能是你需要的,例如unitId = (unsigned short)(atoi(temp)); 请注意,这假设 (a) temp 指向一串数字并且 (b) 数字代表一个可以放入无符号短整型的数字
回答by Tim Schaeffer
Is the pointername
the id, or the string of chars pointed to by name
? That is if name
contains "1234"
, do you need to output 1234
to the file? I will assume this is the case, since the other case, which you would do with unitId = unsigned short(name)
, is certainly wrong.
是指针name
的ID,或者指向字符的字符串name
?也就是说如果name
contains "1234"
,是否需要输出1234
到文件中?我会假设是这种情况,因为您将使用的另一种情况unitId = unsigned short(name)
肯定是错误的。
What you want then is the strtoul()
function.
那么你想要的是strtoul()
功能。
char * endp
unitId = (unsigned short)strtoul(name, &endp, 0);
if (endp == name) {
/* The conversion failed. The string pointed to by name does not look like a number. */
}
Be careful about writing binary values to a file; the result of doing the obvious thing may work now but will likely not be portable.
将二进制值写入文件时要小心;做显而易见的事情的结果现在可能会奏效,但可能无法移植。
回答by Nick Smith
If you have a string (char* in C) representation of a number you must use the appropriate function to convert that string to the numeric value it represents.
如果您有一个数字的字符串(C 中的 char*)表示,您必须使用适当的函数将该字符串转换为它所表示的数值。
There are several functions for doing this. They are documented here: http://www.cplusplus.com/reference/clibrary/cstdlib
有几个函数可以做到这一点。它们记录在此处:http: //www.cplusplus.com/reference/clibrary/cstdlib