Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11222
글쓴 사람
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)
4854heyh...7/6/20179483
4853heyh...7/5/20179584성태님이 작성한대로 해봤습니다. [1]파일 다운로드1
4852김레오7/4/201711765서드파티 dll 디버깅에 대해 질문드립니다. [2]
4851김현준7/3/201710872Datagridview VirtualMode 시 GC가 계속 호출되는 현상이 이해가 안갑니다. [2]
4850heyh...7/3/201710609성태님 밑에 질문드렸던 오류입니다. [1]파일 다운로드1
4849포플러7/2/201729286C#으로 만든 프로그램이 어느 순간 속도가 느려지거나 멈춤현상이 있습니다. [4]
4848윤진영7/2/201712254Microsoft Visual C++ 6.0 무설치 관련 [1]
4847heyh...6/30/201710263아무리 고민해도 답이 안나와서.. 질문 드립니다 [1]
4846heyh...6/28/201710306Clickonce에서 Clickonce로 변수 전달 [1]
4845heyh...6/26/20179978vb.net 에서 manifest 제거 방법 [1]파일 다운로드1
4844san6/21/20179408part3 pdf파일로 보는데 눈아퍼요.... 활자로 보고싶어요 [2]
4843윤현수6/20/20179640socket통신에 관한 질문입니다. [1]
4842오세운6/7/201710325로그인폼 다시 질문드려요. [1]
4841popo6/7/201710090궁금한사항이 있어 질문 드립니다. [1]
4840윤현수6/5/201710100datagridview에 바인딩시킨 dataset.table에 관련된 질문입니다. [2]파일 다운로드1
4839오세운6/2/201710999로그인폼 만들기 [1]
4838후배5/29/201711724PDF Reader 라이브러리.. [3]
4837KDP5/29/201711222c++ to C# 컨버팅 문의 [2]
4836popo5/26/201714361리플렉션 성능 관련 질문 드립니다. [2]
4835guest5/25/201710087wpf Canvas 질문드립니다. [1]
4834부탁드립...5/19/201710990익스체인지 서버 구동중 인증서를 받는 과정에서오류가 납니다 [1]
4833안종윤5/18/201711400winform을 Web Browser에 붙일 수 있는지요? [1]
4832guest5/17/201710864c# UDP socket예제 실행방법 [3]
4831guest5/15/201713349WPF .net 3.5에서 TextBox 한글 문제 [3]
4830초보자5/7/201718262프로그램 비정상 종료 메시지 창 없애는 방법 [4]
4829윤현수4/26/201714640System.Text.Encoding 질문 [1]
... 31  32  33  34  35  36  37  38  39  [40]  41  42  43  44  45  ...