ユニコード変換
code:c
static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
{
uint8_t idx = 0;
for (idx = 0; idx < len; idx++)
{
if (((value >> 28)) < 0xA)
{
pbuf2 * idx = (value >> 28) + '0';
}
else
{
pbuf2 * idx = (value >> 28) + 'A' - 10;
}
value = value << 4;
pbuf2 * idx + 1 = 0;
}
}
code:c
#define UID_BASE 0x1FFFF7E8UL /*!< Unique device ID register base address */
#define DEVICE_ID1 (UID_BASE)
#define DEVICE_ID2 (UID_BASE + 0x4)
#define DEVICE_ID3 (UID_BASE + 0x8)
/**
* @brief Create the serial number string descriptor
* @param None
* @retval None
*/
static void Get_SerialNum(void)
{
uint32_t deviceserial0;
uint32_t deviceserial1;
uint32_t deviceserial2;
deviceserial0 = *(uint32_t *) DEVICE_ID1;
deviceserial1 = *(uint32_t *) DEVICE_ID2;
deviceserial2 = *(uint32_t *) DEVICE_ID3;
deviceserial0 += deviceserial2;
if (deviceserial0 != 0)
{
IntToUnicode(deviceserial0, &USBD_StringSerial2, 8);
IntToUnicode(deviceserial1, &USBD_StringSerial18, 4);
}
}
ユニコード変換関数確認
code:c
#include <stdio.h>
static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
int main() {
uint8_t result18; // テストケース1の結果を格納するバッファ
uint8_t result24; // テストケース2の結果を格納するバッファ
// テストケース1: valueが0x12345678, lenが8の場合
uint32_t testValue1 = 0x12345678;
IntToUnicode(testValue1, result1, 8);
// テストケース2: valueが0xABCD, lenが4の場合
uint32_t testValue2 = 0xABCD;
IntToUnicode(testValue2, result2, 4);
// 結果の表示
printf("テストケース1: ");
for (int i = 0; i < 8; i++) {
printf("%c", result1i);
}
printf("\n");
printf("テストケース2: ");
for (int i = 0; i < 4; i++) {
printf("%c", result2i);
}
printf("\n");
return 0;
}