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

... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...
NoWriterDateCnt.TitleFile(s)
704이호정9/6/200710573        답변글 답변해주신 내용 잘 봤습니다. ^_^b
705정성태9/6/20079485            답변글 [답변]: 답변해주신 내용 잘 봤습니다. ^_^b
706이호정9/7/20079940                답변글 후후... 이러다가 CardSpace 전용게시판 되는 것 아닌지 모르겠습니다. ^_^a;;
693장근배8/29/20079486매핑 구간이 열려 있습니다 [2]
710장근배9/17/20079438    답변글 [답변]: 매핑 구간이 열려 있습니다
692채영사랑8/29/20079182<질문> Ms stress tool 로 부하테스트중.
697정성태9/1/20078885    답변글 [답변]: <질문> Ms stress tool 로 부하테스트중. [1]
690한귀순8/28/20079796ClickOnce 배포 추가질문 [4]
689clev...8/27/20079390SmartClient에서도 Exe COM과 같이 여러곳에서 변수 공유가 가능한가요? [2]
688한귀순8/24/200711626ClickOnce 배포 [1]
686이상욱8/21/200710527[질문] VS.NET 2008 - WCF 를 위한 디버깅 환경 개선 테스트 중에
687정성태8/21/20079113    답변글 [답변]: [질문] VS.NET 2008 - WCF 를 위한 디버깅 환경 개선 테스트 중에
685김동진8/19/200711117Vista에서 WSDL관련 IIs7세팅 질문입니다. [4]
683조스7/31/200710050스마트 클라이언트에서 다른 어셈블리(.NET component) 사용시 에러 [1]
682정해봉7/30/200710975비스타에서 CAS설정하기... 소스 첨부 [1]파일 다운로드1
684정해봉8/14/200710955    답변글 [답변]: 비스타에서 CAS설정하기... 소스 첨부 [3]
681박희진7/27/20079472caspol로 설정된 권한집합 웹에서 읽어올 수 있나요?? [1]
680창민이7/27/200710370COM+등록시 에러 부분 때문에.. [2]
678정해봉7/24/20079310비스타에서 CAS 설정하기... [1]
677첫사랑7/24/20078801SSL에 관한 질문입니다.
679정성태7/26/20079371    답변글 [답변]: SSL에 관한 질문입니다. [1]
676kiuk7/11/20079723웹폐이지상의 이미지 상태 저장을 하고싶습니다. [1]
675안연준7/6/200710151안녕하세요 ^^ [1]
674황태욱7/5/200711250utf-8 환경에서 파라미터 값에 한글이 있을 경우 깨지는 현상이.. [1]
672땡초7/4/200710799Remoting 객체를 호출하는 순간 Timer 의 Elapsed 이벤트가 멈추는 현상 [2]
670조성택7/3/200710327.Net에서 Timer클래스.. [1]
... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...