Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11132
글쓴 사람
KDP (secpkd at gmail.com)
홈페이지
첨부 파일
 

하기 코드 c#으로 구현하고자 합니다. 혹시 컨버팅 해주실수 있는지요~

#include<stdio.h>
#include<stdint.h>

#define CRC16 0x8005

uint16_t gen_crc16(const uint8_t *data, uint16_t size)
{
    uint16_t out = 0;
    int bits_read = 0, bit_flag;


    // test
    printf("buffer in function %s\n", data);

    /* Sanity check: */
    if(data == NULL)
        return 0;

    while(size > 0)
    {
        bit_flag = out >> 15;

        /* Get next bit: */
        out <<= 1;
        out |= (*data >> bits_read) & 1; // item a) work from the least significant bits

        /* Increment bit counter: */
        bits_read++;
        if(bits_read > 7)
        {
            bits_read = 0;
            data++;
            size--;
        }

        /* Cycle check: */
        if(bit_flag)
            out ^= CRC16;

    }

    // item b) "push out" the last 16 bits
    int i;
    for (i = 0; i < 16; ++i) {
        bit_flag = out >> 15;
        out <<= 1;
        if(bit_flag)
            out ^= CRC16;
    }

    // item c) reverse the bits
    uint16_t crc = 0;
    i = 0x8000;
    int j = 0x0001;
    for (; i != 0; i >>=1, j <<= 1) {
        if (i & out) crc |= j;
    }

    return crc;
}

int main()
{
    char buf[]="123456789";
    int c , r;
    printf ("the buf has %s", buf);
    r = gen_crc16(buf,sizeof(buf)-1);
    printf("%04hx\n", r);

    return (0);
}








[최초 등록일: ]
[최종 수정일: 5/29/2017]


비밀번호

댓글 작성자
 



2017-05-29 10시19분
스스로 해보시면 어떨까요? ^^
정성태
2017-05-29 10시39분
[KDP] const UInt16 CRC16 = 0x8005;

        UInt16 gen_crc16(byte[] data, UInt16 size)
        {
            UInt16 _out = 0;
            int bits_read = 0;
            int bit_flag = 0;
            int index = 0;

            // test
            Console.WriteLine("buffer in function {0}\n", data.ToString());

            /* Sanity check: */
            if(data == null)
                return (UInt16)0;

            while(size > 0)
            {
                bit_flag = _out >> 15;

                /* Get next bit: */
                _out <<= 1;
                _out |= (UInt16)((data[index] >> bits_read) & 1); //`enter code here`

                /* Increment bit counter: */
                bits_read++;
                if(bits_read > 7)
                {
                    bits_read = 0;
                    index++;
                    size--;
                }

                /* Cycle check: */
                if(bit_flag > 0)
                    _out ^= CRC16;

            }

            // item b) "push out" the last 16 bits
            UInt16 i;
            for (i = 0; i < 16; ++i) {
                bit_flag = _out >> 15;
                _out <<= 1;
                if(bit_flag > 0)
                    _out ^= CRC16;
            }

            // item c) reverse the bits
            UInt16 crc = 0;
            i = 0x8000;
            UInt16 j = 0x0001;
            for (; i != 0; i >>=1, j <<= 1) {
                if ((i & _out) > 0) crc |= j;
            }

            return crc;
        }
[guest]

... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
5099WPF12/12/201810175안녕하세요. WPF에서 UWP Control을 참조하려고 합니다. [3]파일 다운로드1
5097sdh12/10/201811287[c#] 라이선스 파일 만들기 질문 드립니다. [3]
5096거북이12/3/20187056프로젝트 구성을 참고할 만한 자료가 있을까요? [1]
5095한대현11/21/20187760안녕하세요 c# 설치 파일 빌드중 오류가 생겨서 문의 드려요 [1]파일 다운로드2
5094하주형11/20/20187410안녕하세요 C# using 예약어관련 질문드립니다. [1]
5093Medi...11/19/201810817안녕하세요. wpf Mediaelement 질문 있습니다. [3]
5092하주형11/17/20187386안녕하세요 c# 7.1책 스택관련 질문드립니다. [2]
5091아짱11/15/20188368UWP 개발중 질문이 있습니다. [3]
5090황윤하11/15/201812092c# Socket Server에 접근할 수 있는 client 개수 제한 [5]
5089문성운11/14/20189775uwp에서 TcpListener를 사용할 수 없나요? [5]
5088안중언11/10/20187519TCP 소켓 [1]파일 다운로드1
5084김광흠11/9/20189217사운드 파일 "filename.wav" 와 같은 특정 파일이 실행되는것을 감시하고 싶습니다. [3]
5083거북이11/4/20189519타입의 범위를 넘어서는 연산의 개념을 모르겠습니다. [4]
5082꾸엉11/1/20187618BCL 타입을 모아둔 곳이 있나요? [2]
5081꾸엉10/31/20187753C# 7.1 235p 질문입니다. [3]
5080WPF초보10/30/20189286[WPF] IsManipulationEnabled속성 설정시 Click이벤트 문의 [1]
5079jhp10/30/20188900이 소스에 API후킹 소스를 추가하고 싶은데 어떻게 해야될지 모르겠어요. [4]
5078꾸엉10/29/20187249C# 7.1 185~187p 질문입니다. [2]
5077윤현수10/29/201813676setup파일 설치 이후 실행이 안되는 현상 [11]
5074진우10/28/20187340C# 생성과 동시에 초기화 하는 코드 문의 [2]
5073돌고래10/27/20187427c# 공부 방향 질문 드립니다. [3]
5072엔벌잉10/24/20187958C# textbox, button질문입니다! [4]파일 다운로드1
5071엔벌잉10/23/20188335C#윈도우폼 질문입니다!! [2]
5070진우10/17/20188284Visual Studio 서비스팩과 업데이트 차이 문의 [2]
5069감자10/12/20189018빌드 구성을 재설정하는 방법이 있을까요? [1]파일 다운로드1
5068누오10/10/20187783ASP Core 2.0 에서 dll안에 있는 뷰 읽어들이는 방법? [1]
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...