Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 17501
글쓴 사람
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)
5132존기미3/3/201914588Physical Disk Access문의 [1]
5131강용규2/25/201913870c# 개발자입니다. 타이머 질문드립니다. [1]
5130노인코래방2/25/201918062C#에서 가장 좋은 성능을 보이는 파일 읽고 쓰는 방법이 무엇인가요? [2]
5129진우2/23/201917782닷넷 32비트 기반에서 메모리 부족으로 프로그램이 죽는경우 문의 [2]
5128게스트2/23/201914988안녕하세요. 초보개발자입니다. [3]파일 다운로드1
5127c#2/20/201913975책에 예제 문의드립니다. [2]
5125게스트2/19/201913777delegate를 활용한 event 를 적절히 불러오고 싶습니다. [2]
5124정근화2/12/201913798윈도우 서버2003 환경 오류 [2]
5123김주현2/8/201913621MS LUIS 에 대한 소개 하실 계획이 있으신가요? [1]
5122jaka...2/1/201915715Clickonce 배포 후 Command 실행 [2]파일 다운로드1
5121엔벌이1/31/201915329C# DataGridView의 MDB파일 함수? ArrayList? [1]파일 다운로드1
5120임우진1/30/201916532웹에서 응용프로그램 바로 실행하기 관련 브라우저에서 파라미터가 넘어오지 않습니다.ㅜㅜ [2]
5119guest1/29/201918091교재에 오탈자 있어 알려드리려 합니다 [1]
5118WPF꿈...1/26/201916049GetHashCode 메서드에 대해서 [1]
5117하주형1/25/201915765List<int>에 대한 이해가 잘안됩니다. [5]
5116게스트1/24/201914374asp.net 관련 gridview webform 질문 드립니다. [1]파일 다운로드1
5115Soul...1/24/201914558투명 패널 질문드립니다. [2]
5114박현일1/20/201915176WPF DataContext 관련 초보 질문을 드려봅니다.^^ [5]
5113하주형1/20/201914401안녕하세요 시작하세요 C# 인코딩관련 질문드립니다. [1]
5112손성배1/19/201923689안녕하세요 cp949 인스톨시 오류입니다... 너무 힘들어요 [5]
5111게스트1/10/201914866암호화 라이센스 관련 문의 드립니다. [1]
5110WPF꿈...1/9/201914351Thread Abort 함수 사용시 [2]
5109닷넷개발1/9/201914187thread 관련 질문 예제.. [2]파일 다운로드1
5108닷넷개발1/9/201915394thread 관련 질문 드립니다.. [4]
5107우코아1/4/201918146WPF에서 로딩중 이미지를 구현 - Project [5]파일 다운로드1
5106우코아1/3/201915800WPF에서 로딩중 이미지를 구현 - Source [1]
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...