[2023/03/04] First Start.
소나무 기운 , 전자제품 개발/생산
(C) float를 uint16_t 2개에 나누어 저장하는 방법(To save float by dividing it into two uint16_t)
float 값을 uint16_t 두개에 나누어 넣는 방법은 여러가지가 있을 수 있습니다.
하나의 방법은 float 값을 uint32_t로 변환하고, 그 값을 상위 16비트와 하위 16비트로 나누어 각각 uint16_t 변수에 저장하는 것입니다1. 예를 들면 다음과 같습니다.
There can be several ways to divide the float value into two uint16_t.
One way is to convert the float value to uint32_t, divide it into the top 16 bits and the bottom 16 bits, and store it in the uint16_t variable, respectively, for example:
float f = 3.14; // float값
uint32_t u = *(uint32_t*)&f; // float값을 uint32_t로 변환
uint16_t u1 = u >> 16; // 상위 16비트를 uint16_t로 저장
uint16_t u2 = u & 0xFFFF; // 하위 16비트를 uint16_t로 저장
memcpy를 사용하여 float를 uint16_t로 변환하는 방법(To convert float to uint16_t using memcpy)
float f = 3.14; // float값
uint16_t u[2]; // uint16_t 배열
memcpy(u, &f, sizeof(float)); // float값을 uint16_t 배열에 복사
이렇게 하면 u[0]과 u1에 각각 float값의 상위 16비트와 하위 16비트가 저장됩니다1. memcpy 함수는 메모리의 내용을 원하는 크기만큼 다른 메모리로 복사하는 함수입니다12. 자세한 사용법은 웹 검색을 통해 알아보시기 바랍니다.
This saves the top 16 bits and bottom 16 bits of the float value in u[0] and u1, respectively. 1. The memcpy function is a function that copies the contents of the memory to another memory as large as desired.12. For more information, visit the web search.
공용체를 사용하여 float를 uint16_t로 변환하는 방법(To convert a float to uint16_t using a colloquial)
float f = 3.14; // float값
union { // 공용체 정의
float f; // float형 멤버
uint16_t u[2]; // uint16_t형 배열 멤버
} u;
u.f = f; // 공용체에 float값 저장
이렇게 하면 u.u[0]과 u.u1에 각각 float값의 상위 16비트와 하위 16비트가 저장됩니다. 공용체는 여러 개의 멤버를 가지지만, 메모리는 한 개의 멤버만큼만 할당되는 자료형입니다1. 공용체를 사용하면 서로 다른 자료형을 메모리 단위로 변환할 수 있습니다. 자세한 사용법은 웹 검색을 통해 알아보시기 바랍니다.
If you do this, u.u[0] and u.u1 stores the top 16 bits and the bottom 16 bits of the float value, respectively. A public entity has multiple members, but memory is a type of data that is allocated only as much as one member. 1. You can use a public entity to convert different data types into memory units. For more information, visit the web search.
마무리
float값을 uint16_t 두개에 나누어 넣는 방법은 uint32_t로 변환하거나 memcpy 함수나 공용체를 사용하는 것입니다.
The way to divide the float value into two uint16_t is to convert it to uint32_t or use memcpy functions or co-operatives.
참고문헌
틀린 부분이나 질문은 댓글 달아주세요.
즐거운 하루 보내세요. 감사합니다.
'Python, C, C++' 카테고리의 다른 글
다른 mcu의 구조체 크기 sizeof() 크기가 다른 이유 (0) | 2023.09.07 |
---|---|
'MergeCells' method of a 'TStringGrid' in C++ Builder(C++ Builder에서 'TSring Grid'의 'Merge Cells' 방법) (0) | 2023.03.01 |
strcmp [C][C++][python] (0) | 2022.04.04 |
Example of Python using tkinter module. (0) | 2022.02.24 |
python tkinter 이용 GUI 기본 예제 (0) | 2022.02.23 |
댓글