English 中文(简体)
从Sting到Unsign Long和背部
原标题:Convert from String to Unsigned Long and back
  • 时间:2011-06-16 06:00:32
  •  标签:
  • c++

您如何将体格转换到一个未签字的长处,这样,未签字的长处就代表了8位轨道号码的特性?

提前感谢。

EDIT:我想做的是,在ASCII代码格式中,把4个特性的描述变成了同样的4个特征中的一部分。

最佳回答

在很大程度上,同样的解决办法是,减少对整体类型规模的假设,并检查可加以翻译的上限:

#include <string>
#include <algorithm>
#include <iostream>
#include <climits>

int main()
{
  const size_t MAX = sizeof(unsigned long);

  std::string s("abcd");
  unsigned long res = 0;

  for (size_t i=0; i < std::min(MAX, s.size()); ++i)
  {
    res <<= CHAR_BIT;
    res += (unsigned char) s[i];
  }

  std::cout << std::hex << res;
}

印刷

61626364

插入unsign char的情形是,你方言中含有高度的ASCII,即高于127的数值,在这种情况下,char将是负面的。 Try the string 226226226226” 相反,你会得出不正确的结果。

www.un.org/Depts/DGACM/index_spanish.htm BTW,在你所说的话中,你说的是“和背”,这样,我们就改变了转变:

#include <limits>

std::string tostr(unsigned long x)
{
  // this will mask out the lower "byte" of the number
  const unsigned long mask = std::numeric_limits<unsigned char>::max();

  if (x == 0)
    return std::string("0");

  std::string res;
  for (; x > 0; x >>= CHAR_BIT) {
    res += (char) (x & mask);
  }

  // we extracted the bytes from right to left, so need to reverse the result
  std::reverse(res.begin(), res.end());
  return res;
}

http://ideone.com/Cw7hF"rel=“nofollow> http://ideone.com/Cw7hF

问题回答

Assuming "str" is the string (std::string) and "res" the unsigned long you want to write into. Like this:

for (std::string::iterator i = str.begin(); i != str.end(); ++i) {
    res <<= 8;
    res += *i;
}

但是,它只是为了描述不超过4个特性而努力(假定未签字的最长为32个范围,如果是64个范围,则具有8个特性)

如果你希望以其他方式来填补这一空白,那么,它将把第一种特性放在最重大的预约中。

Edit:res<>/code>不得不自己分配,否则结果就会丧失。





相关问题
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?