如何转换十六进制字符串中的字符串? [英] How to convert a string in hexadecimal string?

查看:123
本文介绍了如何转换十六进制字符串中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++将字符串转换为十六进制,反之亦然





$ b b

尝试我在StackOverflow中找到的示例代码...

  std :: string text =¯ // AF in HEX ASCII。 
std :: ostringstream result;
result<< std :: setw(2)<< std :: setfill('0')<< std :: hex<< std :: uppercase;
std :: copy(text.begin(),text.end(),std :: ostream_iterator< unsigned int>(result,));

result.str()将会是'FFFFFFAF',而不是'AF'



我可以做什么?



我的文本可以有多个字符,例如:text ='¯ABCÈ'; // AF414243C8
convertion之后的结果是:FFFFFFAF414243FFFFFFC8。





我用一个替换解决它...这不是一个好的做法,在这种情况下,但解决了

  if(value> 6)
{// replace
std :: string& str = stringlist2 [count];
const std :: string& from =FFFFFF;
const std :: string& to =;
size_t j;
for(;(j = stringlist2 [count] .find(from))!= string :: npos;){
stringlist2 [count] .replace(j,from.length() ;
}
}


解决方案

不会将 unsigned int 传递给
调用的运算符<< ostream_iterator ;你传递一个 char 。根据您的系统,
char 可能是有符号或无符号的,如果它是有符号和8位(
最常见的情况),它不能包含0xAF(这将是175)。如果
位模式是0xAF,它实际包含的是-0x51(或-81)。其中
将通过将它添加到 UINT_MAX + 1 转换为无符号,其中
为非常大的未签名值。



你可能需要做的是首先将每个字符转换为 unsigned
char
。这将通过添加
UCHAR_MAX + 1 来转换负值;如果你添加0x100 +(0-x51),你会得到0xAF。作为
的正值,所以当它转换为 unsigned (或甚至到
int ),它将保留其值。



通常情况下,当一个人做这种事情时,它在
a时间是一个字符,并且:

  result<< static_cast< int>(static_cast< unsigned char>(ch));使用

。 (注意双重转换。)但你可以这样做

  std :: vector< unsigned char> tmp(text.begin(),text.end()); 

并使用您的呼叫在 tmp 。或使用 std :: transform

  struct ToUnsignedChar 
{
unsigned char operator()(char ch)const
{
return static_cast< unsigned char>(ch);
}
};

std :: transform(
text.begin(),
text.end(),
std :: ostream_iterator< unsigned int& ),
ToUnsignedChar());

(如果你有C ++ 11,这将是一个很好的候选者) p>

Possible Duplicate:
C++ convert string to hexadecimal and vice versa

Trying this sample code which I found in StackOverflow...

std::string text = "¯"; // AF in HEX ASCII. 
std::ostringstream result;  
result << std::setw(2) << std::setfill('0') << std::hex << std::uppercase;  
std::copy(text.begin(), text.end(), std::ostream_iterator<unsigned int>(result, " "));  

result.str() will be 'FFFFFFAF' instead of just 'AF'

What can I do?

My text can have more than one character, like that: text = '¯ABCÈ'; // AF414243C8 The result after 'convertion' is: FFFFFFAF414243FFFFFFC8.

.

Well, I solved it with a replace... it's not a good practice in this case, but solved.

                    if(value > 6)
                    {   //replace
                        std::string& str = stringlist2[count];
                        const std::string& from = "FFFFFF";
                        const std::string& to = "";
                        size_t j;
                        for ( ; (j = stringlist2[count].find( from )) != string::npos ; ) {
                            stringlist2[count].replace( j, from.length(), to );
                        }
                    }

解决方案

You're not passing unsigned int to the operator<< invoked by ostream_iterator; you're passing a char. Depending on your system, char may be signed or unsigned, and if it is signed and 8 bits (the most common case), it cannot contain 0xAF (which would be 175). If the bit pattern is 0xAF, what it actually contains is -0x51 (or -81). Which will be converted to an unsigned by adding it to UINT_MAX+1, which makes for a very large unsigned value.

What you probably need to do is to convert each character to unsigned char first. This will cause negative values to be converted by adding UCHAR_MAX+1; if you add 0x100 + (0-x51), you'll get 0xAF. As a positive value, so when it is converted to unsigned (or even to int), it will retain its value.

Typically, when one is doing this sort of thing, it is one character at a time, and:

result << static_cast<int>( static_cast< unsigned char >( ch ) );

is used. (Note the double conversion.) You could, however, do something like:

std::vector< unsigned char > tmp( text.begin(), text.end() );

and use your call to copy on tmp. Or use std::transform:

struct ToUnsignedChar
{
    unsigned char operator()( char ch ) const
    {
        return static_cast<unsigned char>( ch );
    }
};

std::transform( 
    text.begin(),
    text.end(),
    std::ostream_iterator<unsigned int>( result, " " ),
    ToUnsignedChar() );

(This would be a good candidate for a lambda if you have C++11.)

这篇关于如何转换十六进制字符串中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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