std::string("DEADBEEF")
があるとします.2つの値をstd :: vector(おそらくstd::vector<unsigned char>
、可能であれば)に格納する最もエレガントな方法は、std::vector
のようになります。{ 0xDE, 0xAD, 0xBE, 0xEF }
配列だった?)そしてそれを元に戻す最も良い方法は何でしょうか(std::vector<unsigned char>
- >std::string
)。あなたは非常に高速であることを、この変換が必要な場合は、私はシンプルな無地のために行くだろうstd :: vectorのstd :: stringの16進値をstd :: vectorに格納していますか?
答えて
が
int hexValue(int c)
{
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
return -1;
}
std::vector<unsigned char> toBin(const std::string& s)
{
std::vector<unsigned char> result;
for (int i=0,n=s.size(); i<n-1; i+=2)
{
int x1 = hexValue(s[i]);
int x2 = hexValue(s[i+1]);
if (x1 >= 0 && x2 >= 0)
result.push_back(x1*16 + x2);
}
return result;
}
std::string toHex(const std::vector<unsigned char>buf)
{
const char *hex = "ABCDEF";
int n = buf.size();
std::string result(n*2, ' ');
for (int i=0; i<n; i++)
{
result[i*2] = hex[buf[i]>>4];
result[i*2+1] = hex[buf[i]&15];
}
return result;
}
をループ...
std::vector<unsigned char> toBin(const std::string& s)
{
static unsigned char H[256], L[256];
if (L['1'] == 0)
{
for (int i=0; i<10; i++)
{
H['0'+i] = (i<<4);
L['0'+i] = i;
}
for (int i=0; i<6; i++)
{
H['a'+i] = H['A'+i] = ((10+i)<<4);
L['a'+i] = L['A'+i] = (10+i);
}
}
std::vector<unsigned char> result(s.size()>>1);
for (int i=0,n=s.size(); i<n-1; i+=2)
result[i>>1] = H[s[i]]+L[s[i+1]];
return result;
}
ああ...すみません。私は鞭、針、 '
シンプルで甘い解決策! – likejiujitsu
方法(入力と仮定すると、十分と正式に正しい大きさです) (なしファンシーSTLジャグリングが)について:
#include <string>
#include <vector>
// exception thrown by the encoder
class DecodeError {
public:
DecodeError() {}
DecodeError(const char* s): msg(s) {}
DecodeError(const std::string& s): msg(s) {}
const char* what() { return msg.c_str(); }
private:
std::string msg;
};
class HexEncoder {
public:
void decode(const std::string& str, std::vector<unsigned char>& v);
private:
unsigned char decode(char);
};
void HexEncoder::decode(const std::string& s, std::vector<unsigned char>& v)
{
if (s.size() % 2 != 0)
throw DecodeError("invalid string length");
v.reserve(s.size()/2);
std::string::const_iterator it=s.begin();
while (it != s.end()) {
unsigned char nibble1 = decode(*it++);
unsigned char nibble2 = decode(*it++);
v.push_back(nibble1 << 4 + nibble2);
}
}
unsigned char HexEncoder::decode(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
throw DecodeError("invalid character");
}
int main()
{
std::string s("DEADBEEF");
std::vector<unsigned char> v;
HexEncoder enc;
enc.decode(s, v);
}
for(std::iterator<string> it = str.begin(); it != str.end(); std::advance(it, 2))
{
char tmp;
std::copy(it, it + 2, tmp);
scanf("%02X", &tmp);
vect.push_back(tmp);
}
これは変換の草案ですが、正しく動作するかどうかはわかりません。
どの試みが最も効率的なのだろうか...これはかなりいいね。 –
私のプロジェクトはすでにboostに依存したように、私はboost/algorithm/hex.hppを使用したいが:進への変換
#include <string>
#include <vector>
#include <iterator>
#include <boost/algorithm/hex.hpp>
...
string stringValue("DEADBEEF");
vector<unsigned char> vectorValue;
がString:文字列への変換進のために
boost::algorithm::unhex(stringValue, back_inserter(vectorValue));
:
boost::algorithm::hex(vectorValue, back_inserter(stringValue));
- 1. boost :: interprocess - std :: string vs std :: vector
- 2. std :: vector <std::string>クラッシュ
- 3. std :: std :: stringとstd :: vectorの間の移動<unsigned char>
- 4. std :: vectorのカスタムアロケータ
- 5. C++のstd :: vectorをstd :: vector <unsigned char>に変換する
- 6. C++ std :: vector problems
- 7. std :: vector resize down
- 8. sizeof()std :: vector(C++)
- 9. std :: vectorをstd :: valarrayに代入する
- 10. std :: vectorの問題
- 11. std :: vectorの状態std :: bad_allocの後
- 12. std :: arraysのstd :: vectorの比較関数
- 13. `std :: vector <std :: uint8_t>の高速コピー
- 14. エラー2664 - std :: vector <...>をstd :: tr1 :: shared_ptr
- 15. std :: vectorとstd :: setプロパティを持つコンテナ?
- 16. 内部でstd :: vectorやstd :: listを使わないC++ std :: iterator
- 17. クラスのstd :: Vectorメンバーをエクスポート
- 18. std :: vector - rightアプローチを返す
- 19. コンストラクタとデストラクタのstd :: vector :: clear()
- 20. std :: vector abortの問題
- 21. std :: setとstd :: vectorの違いは何ですか?
- 22. std :: vector <std :: vector <int>> push_backによりヒープバッファオーバーフローが発生する
- 23. クラスコンストラクタでstd :: vectorを設定します
- 24. 定数参照std :: vector
- 25. C++ 11 std :: vector in concurrent environment
- 26. C++ std :: vector emplace vs insert
- 27. 私の関数はstd :: vectorへのポインタ、またはstd :: vectorへの参照を返しますか?
- 28. STD `と`のstd :: vector`を交換::次のように
- 29. イテレータを使用してstd :: listからstd :: vectorを初期化します。
- 30. fstreamをC++のstd :: vectorにロードする
んあなたのstd :: vectorは文字を保持しているか、バイナリデータから/に変換しようとしていますか? –
私の記事を読んでくれてありがとう。バイナリは素晴らしい(固定ポスト#1) –