iOSで困っています。base10 の 10 進数値をリトルエンディアン、16 進数の文字列に変換しようとしています。
これまでのところ、私はそうすることができません。
たとえば、次の整数をリトル エンディアンの16 進数に変換したいと思います。
int 値 = 11234567890123456789112345678911;
iOSで困っています。base10 の 10 進数値をリトルエンディアン、16 進数の文字列に変換しようとしています。
これまでのところ、私はそうすることができません。
たとえば、次の整数をリトル エンディアンの16 進数に変換したいと思います。
int 値 = 11234567890123456789112345678911;
次のようにできます。
#include <stdio.h>
#include <string.h>
void MulBytesBy10(unsigned char* buf, size_t cnt)
{
unsigned carry = 0;
while (cnt--)
{
carry += 10 * *buf;
*buf++ = carry & 0xFF;
carry >>= 8;
}
}
void AddDigitToBytes(unsigned char* buf, size_t cnt, unsigned char digit)
{
unsigned carry = digit;
while (cnt-- && carry)
{
carry += *buf;
*buf++ = carry & 0xFF;
carry >>= 8;
}
}
void DecimalIntegerStringToBytes(unsigned char* buf, size_t cnt, const char* str)
{
memset(buf, 0, cnt);
while (*str != '\0')
{
MulBytesBy10(buf, cnt);
AddDigitToBytes(buf, cnt, *str++ - '0');
}
}
void PrintBytesHex(const unsigned char* buf, size_t cnt)
{
size_t i;
for (i = 0; i < cnt; i++)
printf("%02X", buf[cnt - 1 - i]);
}
int main(void)
{
unsigned char buf[16];
DecimalIntegerStringToBytes(buf, sizeof buf, "11234567890123456789112345678911");
PrintBytesHex(buf, sizeof buf); puts("");
return 0;
}
出力 ( ideone ):
0000008DCCD8BFC66318148CD6ED543F
結果のバイトを 16 進数の文字列に変換することは (それが必要な場合) 簡単です。
他の問題は別として (他の人によってすでに指摘されているので、繰り返しません)、エンディアンを交換する必要がある場合は、クロスプラットフォームで何かを行っている (または別の例でオーディオサンプル形式を使用している) とします。そのために、Core Foundation によって提供される関数がいくつかありますCFSwapInt32HostToBig()
。
これらの関数の詳細については、Byte-Order Utilities Referenceページをチェックしてください。探しているものが見つかるかもしれません。
答え: できません。この数には、128 ビットの整数が必要です。