const char*
16進値を含む8文字の配列(より大きな文字列の一部である可能性があります)を指す、があります。これらの文字を4の配列に変換する関数が必要ですuint8_t
。ここで、ソース配列の最初の2文字が、ターゲット配列の最初の要素になります。たとえば、私がこれを持っている場合
const char* s = "FA0BD6E4";
に変換してほしい
uint8_t i[4] = {0xFA, 0x0B, 0xD6, 0xE4};
現在、私はこれらの機能を持っています:
inline constexpr uint8_t HexChar2UInt8(char h) noexcept
{
return static_cast<uint8_t>((h & 0xF) + (((h & 0x40) >> 3) | ((h & 0x40) >> 6)));
}
inline constexpr uint8_t HexChars2UInt8(char h0, char h1) noexcept
{
return (HexChar2UInt8(h0) << 4) | HexChar2UInt8(h1);
}
inline constexpr std::array<uint8_t, 4> HexStr2UInt8(const char* in) noexcept
{
return {{
HexChars2UInt8(in[0], in[1]),
HexChars2UInt8(in[2], in[3]),
HexChars2UInt8(in[4], in[5]),
HexChars2UInt8(in[6], in[7])
}};
}
これが私がそれをどこから呼んだかです:
const char* s = ...; // the source string
std::array<uint8_t, 4> a; // I need to place the resulting value in this array
a = HexStr2UInt8(s); // the function call does not have to look like this
私が疑問に思っているのは、これを行うためのより効率的な(そしてポータブルな)方法はありますか?たとえば、返すのstd::array
は良いことですか、それともdst
ポインタを渡す必要がありますHexChars2UInt8
か?または、私の機能を改善する他の方法はありますか?
私がこれを求めている主な理由は、ある時点でこれを最適化する必要がある可能性があり、将来API(関数プロトタイプ)が変更された場合に問題が発生するためです。