Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 11255
글쓴 사람
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)
3634Hyun...11/2/201510898c# 에서 webkit browser에서 webgl을 이용하는 사이트에 접속이 안됩니다. [1]
3633힘찬도약10/31/201511562mysql insert where not exists [6]
3632힘찬도약10/27/201512199C# Lock 관련해서 질문드립니다. [6]
3655iwc11/30/201510345    답변글 [답변]: C# Lock 관련해서 질문드립니다.
3631강준10/26/201513183iis 8.5 preload 기능에 대해 질문이 있습니다. [9]
3630김정훈10/25/201511588몬티홀 게임 관련 질문 [1]
3629pooq10/23/201512102리플렉션 관련해서 질문 입니다. [3]
3628최영민10/22/201511049스마트 클라이언트 로딩속도 문의입니다. [3]
3627양주호10/22/201510861C#으로 컨버팅 하려고 하는데요... [1]
3626조성진10/21/201512005책보고 첫번째 예제부터 문제가 생기네요 ^^; [4]파일 다운로드1
3623Bere...10/19/201512036질문이라기 보단... [2]
3625Bere...10/20/201511544    답변글 [답변]: 질문이라기 보단... [2]파일 다운로드1
3621힘찬도약10/18/201511633[C# 6.0]multi threading과 ui control [9]
3624힘찬도약10/19/201511522    답변글 [답변]: [C# 6.0]multi threading과 ui control [6]파일 다운로드1
3620popo10/13/201510483WPF의 datagrid, listview 컨트롤 관련 질문 입니다. [1]
3619링크의 ...10/12/201514996OCX 로드 관련 질문입니다. [5]파일 다운로드1
3616수요일밥...10/7/201515356몇 가지 오류 (2) [6]
3615김응규10/7/201510726다시한번 질문 드립니다. (이번엔 자세하게 기술했습니다.) [1]
3614김응규10/6/20159590안녕하세요. wcf net.tcp 관련 질문 하나만 올려요~~ [4]
3613강준10/5/201514395IIS Application Pool 시작/중단 에 대한 이벤트 로그는 어디에 남나요??? [2]
3612심심한일...10/4/201516360몇 가지 오류 [4]
3611나그네9/30/201510725안녕하세요 답글을 이제 보았습니다. [3]
3608기차니9/21/201511504컬럼이 많은 데이터그리드에서 정렬 할 때 속도가 느립니다. [3]
3609기차니9/22/201511615    답변글 [답변]: 컬럼이 많은 데이터그리드에서 정렬 할 때 속도가 느립니다. [1]파일 다운로드1
3610기차니9/23/201512474        답변글 [답변]: [답변]: 컬럼이 많은 데이터그리드에서 정렬 할 때 속도가 느립니다. [5]파일 다운로드1
3606guest9/20/201511069확장 클래스 네이밍 규칙은 어떻게 하시나요? [1]
... 46  47  [48]  49  50  51  52  53  54  55  56  57  58  59  60  ...