Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11126
글쓴 사람
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)
5040농상8/14/20188609정규표현식에 대해서 [3]
5039유영태8/14/201810168비동기 소켓사용후 해당 스레드가 남아있을때 처리 방법 [4]파일 다운로드1
5038이정석8/9/20188308서버파일 로컬파일 비교 [2]
5037jjh8/8/201811705c# 으로 화면캡쳐해서 동영상으로 만드는앱을 만들고싶습니다 [6]
5036볼딱지8/8/20189535C# programming 개발 관련 질문이 있습니다. [1]
5035궁금합니...8/8/201810207C# 버전 고민 어떤 책을 사야하나요? [1]
5034신동열8/8/20188605덤프 파일 분석 관련해서 문의 드려요. [1]
5033최규성8/7/201810702C# WinForm, Oracle 9i 로 프로그래밍시 ODP.NET, 배포 설정 방법 질문입니다. [6]
5032이정석8/6/201812532C# 으로 만들 dll 등록 [7]
5031J.S.8/6/20188186System.Array class의 구조에 대한 질문 [1]
5030농상8/4/20188726람다 식을 이용한 메서드 정의 확대를 공부하고 있습니다. [2]
5029Soul...8/3/20188230C# ActiveX 컨트롤 질문드립니다. [4]
5028도토리8/1/20188748혹시 회사에서 TFS 구축시 SQL-Server 라이선스 정책 아시는분 계신지요? [2]
5027농상7/31/20189562Freachable Queue의 발음을 표기하면 어떨까요? [2]
5026농상7/31/201812001소멸자의 이름에 대해서 [2]
5025김기철7/30/20189476selenium 질문좀 드릴개요 [3]
5024농상7/29/20188722패턴 매칭 when에 대해서 질문있습니다. [2]
5023농상7/28/201811690패턴매칭에 대해서 질문 있습니다. [1]
5022농상7/28/20188720튜플에 대해서 건의가 있습니다. [1]
5021농상7/27/20188222예외필터에 대해서 질문이요 [4]
5020농상7/27/201811081null 조건 연산자 예제에서 잠깐 혼동이 일어났습니다. [1]
5019농상7/26/201813404오버플로우와 언더플로우 [2]
5018이재윤7/26/20189820정말 간단한 질문일 수 있는데요! [1]
5017나나7/24/201813281Stopwatch.ElapsedTicks와 Stopwatch.ElapsedMilliseconds [9]
5015지누7/18/201810291ASP.Net Core 를 안드로이드에서 작동하는게 가능한가요? [1]
5016지누7/19/20187459    답변글 [답변]: ASP.Net Core 를 안드로이드에서 작동하는게 가능한가요?
... 31  32  [33]  34  35  36  37  38  39  40  41  42  43  44  45  ...