English 中文(简体)
如何发现一名Integer在其代理中是否有信件
原标题:How to find if an Integer has letters in its hex representation
  • 时间:2011-11-23 08:45:25
  •  标签:
  • c++
  • stream

I am trying to write a method in C++ which determines if an integer has letters in its hex representation using stream manipulators without using any for loop. One way could be to use getline and do some math. Can someone provide hints on the kind of computation required?

我赞赏您的帮助或利用《意向书》,但我想到的是一点,这并不令人信服地证明了这一点。

最佳回答

你们可以做一些事情(只有逻辑操作和一种次举)

 bool has_letters(int num)
 {   
      if(num < 0) num = 0- num; //abs value
      unsigned char * c;
      c = (unsigned char * )(& num)
      for(int i=0;i<sizeof(int),i++)
      {
           if(((c[i] & 0xf) > 0x9) || ((((c[i]>>4) & 0xf) > 0x9) )
               return true;
           //or you can use precomputed table
           //if(table[c[i]])
           //     return true;
      }
      return false;
 }

Trick is that binary representaion of string is allready hex (attle) You Just need to chek each nibble

问题回答

使用C++ostringstreamstring:

bool hasHexDigits(int number)
{
    std::ostringstream output;
    output << std::hex << number;
    return output.str().find_first_of("abcdefABCDEF") != string::npos;
}

EDIT:其他解决办法,不使用溪流。 它提高了业绩(没有分支机构)和记忆明智(没有拨款),但对于你的“家务”来说可能过于先进:

template<int I> struct int_to_type
{
    static const int value = I;
};

inline bool __hasHexCharacters(int number, int_to_type<0>)
{
    return false;
}

template <int digitsLeft>
inline bool __hasHexCharacters(int number, int_to_type<digitsLeft>)
{
   return ((number & 0xF) > 9) | __hasHexCharacters(number >> 4, int_to_type<digitsLeft-1>());
}

inline bool hasHexCharacters(int number)
{
    return __hasHexCharacters(number, int_to_type<2 * sizeof(int)>());
}

在你达到零之前,把 in隔16个。 每次检查其余时间,如果检查结果前后不一;有9 he。

sprintf(string,"%x",<your integer>);

页: 1

之后,如果你使用一些可动用的职能在下文中打字。

a,b,c,d,e,f
// Assumes 32-bit int. Computing the mask based on UINT_MAX is left as an
// exercise for the reader.

int has_hex_alpha(unsigned int num) {
    return !!((num & (num << 1 | num << 2)) & 2290649224);
}

Your requirements are very weird, so it s hard to give a correct answer. All the solutions so far seems to either iterate ( although some inside std functions ), or work with a straight up integer, which seems to go against your requirements? "hex representation" suggests to me that you have the number in the form of a string. If this is the fact then not using for loops(?) & forcing us to use stream manipulators makes it a no go. If the representation is in form of a ascii string, and we re not allowed to iterate, then one solution which doesn t require neither iteration nor conversion ( which likely will iterate in itself ) can make use of the fact that all alpha numeric chars have at least one of the 2 MSBs set:

#include <iostream>
#include <string>
#include <cassert>
#include "boostcstdint.hpp"
union StrInt
{
    boost::uint64_t val;
    char str[ sizeof( boost::uint64_t ) ];
};

int main()
{
    std::string someString("A9999999" );
    StrInt tmp;
    assert( someString.size() <= sizeof( tmp.str ) );
    memcpy( tmp.str, &someString[0], someString.size() );
    std::cout << !!( tmp.val & 0xC0C0C0C0C0C0C0C0ul ) << std::endl;
}

也许使用reg?

regex rgx("[A-Za-z]+");
smatch result;    
if(regex_search(integer_string, result, rgx)) { 
  cout << "There is alpha chars in the integer string" << endl;
}
bool func(int num) {
    while ( num > 0 ) {
         if ( num % 16 > 9 ) return true;
         num /= 16;
    }
    return false;
}

另一种使用下游的方法:

std::stringstream ss;
std::string str;
std::string::iterator it;
bool hasChar = false;

// Use the hex modifier to place the hex representation of 75 into the
// stream and then spit the hex representation into a string.
ss << std::hex << 75;
str = ss.str(); // str contains "4b"
it = str.begin();

// Check for characters in hex representation.
while (!hasChar && it != str.end())
{
    if (isalpha(*it))
        hasChar = true;
    ++it;
}

利用流操纵者,例如:

bool
hasAlphaInRepresentation( unsigned number )
{
    std::ostringstream s;
    s << std::hex << number;
    std::string str = s.str();
    return std::find( str.begin(), str.end(), IsAlpha()) != str.end();
}

would do the trick. (I m assuming that you have an IsAlpha functional object in your toolbox. If not, it s fairly straightforward to implement, and it s always useful.)

当然,如果唯一的要求没有漏洞:

bool
hasAlphaInRepresentation( unsigned number )
{
    return number != 0
        && (number % 16 > 9 || hasAlphaInRepresentation( number / 16 ));
}

:-

你们可以首先在扼杀性流中储存一只牛肉,并试图将其变成一种mal。 如果它不能转换,那么我们可以说,在给定的愤怒中,有信件存在。

    stringstream myStream;
    int myInt;
    cin>>myInt;
    myStream<<hex<<myInt;
    myStream>>dec>>myInt;
    cout<<myStream.fail()<<endl;




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签