Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사],
조회: 19583
글쓴 사람
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)
1189Youn...12/10/201319070책을 사기전에 궁금한것이 있습니다. [1]
1188이민석12/5/201320435ocx 를 C#에서 마샬링관련 질문입니다.. [2]파일 다운로드1
1187이성환12/3/201321737WPF WebBrowser control의 자식 창이 close 되기 전 Navgate 재호출 문제 [2]파일 다운로드1
1186박종혁12/2/201319852책의 예제 중에 result 변수가 할당 되었지만 사용되지 않았다고 오류가 납니다!! [1]
1185박은희11/27/201322067멀티바이트로 개발한 프로그램을 유니코드로 변경시 쉽게 처리 하는법 [2]파일 다운로드1
1183박현수11/20/201318901WCF에서 web.config appsetting 읽기 [1]
1184박현수11/20/201320650    답변글 [답변]: WCF에서 web.config appsetting 읽기 [3]파일 다운로드1
1182유창우11/16/201329608자마린이 궁금... [8]
1181허관11/11/201318873책 샀습니다! [1]
1180김형지11/6/201323392안녕하십니까. 프로그램이 실행되지 않아 여쭙고자 합니다ㅠ [1]파일 다운로드1
1179이민석11/4/201323446[긴급질문] [in,out] 배열을 C# 에서 C/C++ 로 넘기는 방법 - 두번째 이야기 관련 질문.. [6]파일 다운로드1
1178박진영11/1/201322663[급질문] IIS 하위 가상폴더 구성 문의 [4]
1177Jeon...10/28/201319057안녕하세요~ 어머니께 물어서 사이트를 찾아왔어요 [2]
1176김태훈10/25/201319388AxWebBrowser에 대해 질문드립니다. [1]
1175서경희10/20/201324362netscape 지원이 되지 않는다는 문구.. [2]파일 다운로드1
1174임동찬10/16/201323723프리징 현상에 대한 고민 [5]
1173김재영10/8/201317930인터페이스에 대해 기초적 질문이 있습니다. [2]파일 다운로드1
1172박진영10/2/201321081웹사이트 연결시 AJAX 어셈블리 오류 문의드립니다. [5]파일 다운로드1
1171링거8/30/201329458ClickOnce 업데이트 문제. [4]
1170임동찬8/28/201320327비동기적 이벤트 핸들링 방법 [2]
1167나종식8/20/201319069win7 에서 LSP 가 DNS Client 에 인젝션 안되는 문제 [6]
1165임동찬8/19/201318507오류 발생시 로깅 문제.. [3]
1164박철8/19/201319227모바일 게임서버를 작성 하려면 무엇부터 시작하여야 하나요? [2]
1163안연준8/2/201318538음... 안녕하세요 ^^ 윈도우즈 폼에 대해서 잠시 물어볼께요 [3]
1162박진영7/23/2013169291개의 PC에서 동일사이트 접속제한을 어떻게 하죠? [1]
1161Ji Y...7/12/201319772안녕하세요? 음성인식 관련해서 질문있습니다, [2]
... 46  47  48  49  50  51  52  53  54  55  56  57  58  [59]  60  ...