I understand that one can convert a string to a character array like so:
我知道可以将字符串转换为字符数组,如下所示:
string a = "abcdefgh";
char b[8];
strcpy(b, a.c_str());
cout << (int)b[3];
Here I get the output 100
.
这里我得到输出100。
My questions is: How can I convert the string into an array of long
. I am wondering how I can convert for example the string "abcdefgh" into an array long b[2]
. The first long (b[0]
) should be the long 0x61626364
and the second (b[1]
) should be 0x65666768
. If that makes sense. So
我的问题是:如何将字符串转换成一个长数组。我想知道如何将字符串“abcdefgh”转换为一个长b[2]的数组。第一个long (b[0])应该是long 0x61626364,第二个long (b[1])应该是0x65666768。如果这是有意义的。所以
cout << (unsigned int)b[0]
should output 1001633837924
.
应该输出1001633837924。
2 个解决方案
#1
3
Try the following
试试以下
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
std::string s( "abcdefgh" );
long b[2] = {};
for ( std::string::size_type i = 0, j = 0; i < 2 && j < s.size(); j++ )
{
b[i] = b[i] << 8 | ( unsigned char)s[j];
if ( j % sizeof( long ) == sizeof( long ) - 1 ) i++;
}
std::cout << std::hex << b[0] << '\t' << b[1] << std::endl;
return 0;
}
The output is
输出是
61626364 65666768
It would be even better to substitute statement
用代换语句会更好
if ( j % sizeof( long ) == sizeof( long ) - 1 ) i++;
for
为
if ( j % sizeof( *b ) == sizeof( *b ) - 1 ) i++;
In this case you could change the type of b without changing all other code. For example
在这种情况下,您可以更改b的类型,而无需更改所有其他代码。例如
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
std::string s( "abcdefgh" );
long long b[2] = {};
for ( std::string::size_type i = 0, j = 0; i < 2 && j < s.size(); j++ )
{
b[i] = b[i] << 8 | ( unsigned char)s[j];
if ( j % sizeof( *b ) == sizeof( *b ) - 1 ) i++;
}
std::cout << std::hex << b[0] << '\t' << b[1] << std::endl;
return 0;
}
The output is
输出是
6162636465666768 0
#2
1
You can use reinterpret_cast
if your system uses the right endianess.
如果您的系统使用了正确的endianess,那么可以使用reinterpret_cast。
For example (it's not your expected output):
例如(它不是你的期望输出):
std::string a = "abcdefgh";
const long* b = reinterpret_cast<const long*>(a.data());
std::cout << std::hex << b[0] << " " << b[1] << std::endl;
// 64636261 68676665
If you want to get the other one, you'll have to code it yourself or use byte swap operations. Example with MSVC:
如果你想要得到另一个,你必须自己编码或者使用字节交换操作。与MSVC的例子:
#include<Bits.h>
// ....
std::cout << std::hex << _byteswap_ulong(b[0]) << " " << b[1] << std::endl;
// 61626364 68676665
It's easy to build the result with std::transform
:
使用std::transform容易构建结果:
std::string a = "abcdefgh";
const long* b = reinterpret_cast<const long*>(a.data());
long c[2];
std::transform(b, b+2, c, _byteswap_ulong);
std::cout << std::hex << c[0] << " " << c[1] << std::endl;
// 61626364 65666768
#1
3
Try the following
试试以下
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
std::string s( "abcdefgh" );
long b[2] = {};
for ( std::string::size_type i = 0, j = 0; i < 2 && j < s.size(); j++ )
{
b[i] = b[i] << 8 | ( unsigned char)s[j];
if ( j % sizeof( long ) == sizeof( long ) - 1 ) i++;
}
std::cout << std::hex << b[0] << '\t' << b[1] << std::endl;
return 0;
}
The output is
输出是
61626364 65666768
It would be even better to substitute statement
用代换语句会更好
if ( j % sizeof( long ) == sizeof( long ) - 1 ) i++;
for
为
if ( j % sizeof( *b ) == sizeof( *b ) - 1 ) i++;
In this case you could change the type of b without changing all other code. For example
在这种情况下,您可以更改b的类型,而无需更改所有其他代码。例如
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
std::string s( "abcdefgh" );
long long b[2] = {};
for ( std::string::size_type i = 0, j = 0; i < 2 && j < s.size(); j++ )
{
b[i] = b[i] << 8 | ( unsigned char)s[j];
if ( j % sizeof( *b ) == sizeof( *b ) - 1 ) i++;
}
std::cout << std::hex << b[0] << '\t' << b[1] << std::endl;
return 0;
}
The output is
输出是
6162636465666768 0
#2
1
You can use reinterpret_cast
if your system uses the right endianess.
如果您的系统使用了正确的endianess,那么可以使用reinterpret_cast。
For example (it's not your expected output):
例如(它不是你的期望输出):
std::string a = "abcdefgh";
const long* b = reinterpret_cast<const long*>(a.data());
std::cout << std::hex << b[0] << " " << b[1] << std::endl;
// 64636261 68676665
If you want to get the other one, you'll have to code it yourself or use byte swap operations. Example with MSVC:
如果你想要得到另一个,你必须自己编码或者使用字节交换操作。与MSVC的例子:
#include<Bits.h>
// ....
std::cout << std::hex << _byteswap_ulong(b[0]) << " " << b[1] << std::endl;
// 61626364 68676665
It's easy to build the result with std::transform
:
使用std::transform容易构建结果:
std::string a = "abcdefgh";
const long* b = reinterpret_cast<const long*>(a.data());
long c[2];
std::transform(b, b+2, c, _byteswap_ulong);
std::cout << std::hex << c[0] << " " << c[1] << std::endl;
// 61626364 65666768