Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11185
글쓴 사람
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)
4773김상호11/4/201613434재귀함수 반복문 변환 [1]파일 다운로드1
4772자연인10/27/201614421hwpctrl을 사용하는 사이트에서 나와 브라우저를 종료하면 오류메세지가 나옵니다. [1]파일 다운로드1
4771문종훈10/18/201614461.net 소스 질문이 있습니다 [2]
4770누구게~...10/15/201611691세도나 [1]
4769spow...10/13/201610516올리시는 게시물에 '좋아요'를 선택할 수 있도록 해주세요 [3]
4768브라운10/11/201612254질문 하나만 드려도 될까요 [4]
4767암호군10/4/201616617c# aes 128 암복호화 관련 문의드립니다. [3]
4766김신철9/29/201611478Visual Studio 2015에서 .net 3.5로 c# 6.0 사용시 문제점에 대해서 궁금합니다. [1]
4765spow...9/23/201610897참조를 통해 속성의 값을 변경하고 싶을 때 우아한 코딩 방법이 있을까요? [2]
4764지현명9/22/201612440Visual Studio 2008 c#에서 추가된 솔류션의 디버깅이 안걸립니다. [2]파일 다운로드1
4763송기태9/20/201611211안녕하세요! 질문이 있어 문의드립니다! [1]파일 다운로드1
4762김신철9/20/201612342Visual Studio 2015 마이그레이션 후 빌드 및 에러 문제.. 도와주세요~ [2]
4761JH9/19/201612753WPF로 Viewbox 사용 시 폰트 크기 일정화 여부 [1]
4760초보9/18/201612694유닉스서버(HP)에서 C# 서버 프로그램 실행 가능 한지요? [1]
4759dev009/16/201613492Queue out of memory [3]
4758임기성9/12/201613064MS오피스 워드 64비트에서 32비트 COM개체 사용방법 문의 [2]
4757조영준9/7/201611099DLL 후킹과 관련해서 질문이 있습니다. [2]
4756Kim ...9/6/201613155drag&drop 관련해서 문의 드립니다. [6]
4755stel...9/4/201611998안녕하세요! 윈도우 창에 관련되서 질문입니다.! [3]
4754초보개발자8/25/201610889UWP 의 적용 범위에 대해서 어떻게 생각하십니까? [1]
4753조호찬8/23/201615761sybase 의 한글 가져오기 문의 [7]
4752타미플루8/19/201611377IIS 로그에서 time-taken이 0이 나올수 있나요? [4]
4751김민석8/16/201611569가변크기의 구조체를 SendMessage로 타 프로세스에 전송하는 방법이 있을까요? [1]
4750강준8/13/201613106ElementHost Memory Leak 현상 (아래내용과 동일 첨부 추가^^) [5]파일 다운로드1
4749강준8/11/201612120ElementHost Memory Leak 현상 [6]
4748Bere...8/3/201610977그냥 생각이 들어서 여기 글 써봅니다. [1]
... 31  32  33  34  35  36  37  38  39  40  41  42  [43]  44  45  ...