*sz
is the same as sz[0]
, i.e. the first character of the string. In your case the first character has the numeric value 33 (aka hex 0x21). In other words, your code prints 33.
If you want to convert the first sizeof(int)
characters to an integer, you should use bit shifting instead.
If sizeof(int)
is 4 and you want little endian conversion, it would be something like:
int n = (sz[3] << 24) + (sz[2] << 16) + (sz[1] << 8) + sz[0];
note: depending on whether char is signed or unsigned, you may have to add a cast to unsigned before shifting.
note: Instead of +
you can also use |
(i.e. bitwise OR).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…