Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 19585
글쓴 사람
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]

... 46  47  48  49  50  51  52  53  [54]  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1343h11/2/201420835안녕하세요 [1]
1342왕초보11/1/201425291C#에서의 함수포인터 [13]
1347왕초보11/5/201420019    답변글 [답변]: C#에서의 함수포인터 [1]
1341영택10/29/201421420MFC Amazon S3 사용관련하여 질문드립니다. [2]
1344영택11/4/201417763    답변글 [답변]: MFC Amazon S3 사용관련하여 질문드립니다. [2]
1340감사합니다10/28/201422509c# wpf shdocvw internet explorer를 윈도우프레임으로 집어 넣을 수 있는지 궁금합니다. [3]파일 다운로드1
1338헬프미10/28/201423390tcp 소켓의 SYN_RECV 상태에 대하여 질문있습니다. [3]
1336이소정10/27/201418271filestream seek에 대해 질문있습니다. [1]
1337이소정10/28/201419355    답변글 [답변]: filestream seek에 대해 질문있습니다.파일 다운로드1
1339이소정10/28/201422406        답변글 [답변]: [답변]: filestream seek에 대해 질문있습니다. [2]파일 다운로드2
1335황지혜10/24/201423002psexec 관련 질문드립니다. [1]
1334dell10/24/201418881여기가 있는줄 모르고 방명록에 남겼네요 ㅠㅠ 방명록 한번만 봐주시면 안될지요? [1]
1333왕초보10/24/201419311Bitmap 질문 [5]
1332김동진10/16/201425937clickonce 와 smartscreen filter 기능에 대해서 [2]
1331궁굼합니다10/8/201419408loopback adapter 자동 설치가 가능 한가요? [2]
1330이현중10/8/201422169파일 다운로드 방법 문의 [2]파일 다운로드1
1329popo10/2/201419484싱글톤과 싱글톤 소멸자에 대해 궁금점이 있습니다. [1]
1328달쏭9/25/201420500WCF Service측에서 parameter 값이 null이 오는 이유를 알고 싶어요. [2]
1326궁굼합니다9/19/201419429VB의 Shell 기능과 완벽히 똑같은 기능의 함수 [4]
1325지현태9/18/201419228WPF 의존속성과 바인딩에 대한 질문입니다.. [1]파일 다운로드1
1324popo9/17/201418097wcf 질문 드립니다. [1]
1323Anon...9/17/201420896인스턴스와 객체의 차이 [1]
1322김영대9/16/201420678죄송하지만 .NET 관련 질문게시판이지만 혹시 ASP.NET MVC에 대해서 질문 드려도 됩니까 (__);; [2]
1321부탁드립...9/15/201422984윈도우 어플리케이션 개발 방법(?) 에대해(직접적인 코드라기보단 프로젝트 방향??) [14]
1320소켓서버...9/13/201423395서로다른 포트번호로 로컬 Socket 서버를 개설시 Close 문제 입니다. [2]
1319ㅇㅇ9/12/201423268134쪽 예제 오타 [3]
... 46  47  48  49  50  51  52  53  [54]  55  56  57  58  59  60  ...