uint8_t/uint16_t/uint32_t/uint64_tデータ型
2276 ワード
これらのデータ型はC 99で定義され、具体的には:/usr/include/stdint.h ISO C99: 7.18 Integer types
出力のフォーマット:
unit64_t %llu
unit32_t %u
unit16_t %hu
注意:
気をつけてねt型変数の出力、例えば次のコードは、何を出力しますか?
uint8_t fieldID = 67; cerr<< "field="<< fieldID <
その結果、私たちが考えているfield=67ではなくfield=Cであることがわかりました.
これはtypedef unsigned char uint 8_t;
uint8_tは実際にはcharであり、cerr<<出力ASCIIコードは67という数字ではなく67の文字である.
したがってuint 8_を出力するt型の変数は、実際には、実際の数字ではなく、対応する文字を出力する.
67を出力するには、次のようにします.
cerr<< "field="<< (uint16_t) fieldID <
結果:field=67
同様:uint 8_t型変数が文字列に変換され、文字列がuint 8_に変換されます.tタイプ変数はすべて注意してください.uint 8_t型変数が文字列に変換されるとASCIIコードに対応する文字が得られ、文字列がuint 8_に変換されるt変数の場合、文字列の最初の文字が変数に付与.
たとえば、次のコードがあります.
[cpp] view plain
copy
#include
#include
#include
using namespace std;
int main()
{
uint8_t fieldID = 67;
// uint8_t --> string
string s;
ostringstream strOStream;
strOStream <
s = strOStream.str();
cerr <
// string --> uint8_t
s = "65";
stringstream strStream;
strStream <
strStream >> fieldID;
strStream.clear();
cerr <
}
上記のコードは、次のように出力されます.
C
6
/* There is some amount of overlap with as known by inet code */
#ifndef __int8_t_defined
# define __int8_t_defined
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
# if __WORDSIZE == 64
typedef long int int64_t;
# else
__extension__
typedef long long int int64_t;
# endif
#endif
/* Unsigned. */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
#ifndef __uint32_t_defined
typedef unsigned int uint32_t;
# define __uint32_t_defined
#endif
#if __WORDSIZE == 64
typedef unsigned long int uint64_t;
#else
__extension__
typedef unsigned long long int uint64_t;
#endif
出力のフォーマット:
unit64_t %llu
unit32_t %u
unit16_t %hu
注意:
気をつけてねt型変数の出力、例えば次のコードは、何を出力しますか?
uint8_t fieldID = 67; cerr<< "field="<< fieldID <
その結果、私たちが考えているfield=67ではなくfield=Cであることがわかりました.
これはtypedef unsigned char uint 8_t;
uint8_tは実際にはcharであり、cerr<<出力ASCIIコードは67という数字ではなく67の文字である.
したがってuint 8_を出力するt型の変数は、実際には、実際の数字ではなく、対応する文字を出力する.
67を出力するには、次のようにします.
cerr<< "field="<< (uint16_t) fieldID <
結果:field=67
同様:uint 8_t型変数が文字列に変換され、文字列がuint 8_に変換されます.tタイプ変数はすべて注意してください.uint 8_t型変数が文字列に変換されるとASCIIコードに対応する文字が得られ、文字列がuint 8_に変換されるt変数の場合、文字列の最初の文字が変数に付与.
たとえば、次のコードがあります.
[cpp] view plain
copy
#include
#include
#include
using namespace std;
int main()
{
uint8_t fieldID = 67;
// uint8_t --> string
string s;
ostringstream strOStream;
strOStream <
s = strOStream.str();
cerr <
// string --> uint8_t
s = "65";
stringstream strStream;
strStream <
strStream >> fieldID;
strStream.clear();
cerr <
}
上記のコードは、次のように出力されます.
C
6