Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11229
글쓴 사람
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)
5019농상7/26/201813519오버플로우와 언더플로우 [2]
5018이재윤7/26/20189884정말 간단한 질문일 수 있는데요! [1]
5017나나7/24/201813398Stopwatch.ElapsedTicks와 Stopwatch.ElapsedMilliseconds [9]
5015지누7/18/201810372ASP.Net Core 를 안드로이드에서 작동하는게 가능한가요? [1]
5016지누7/19/20187522    답변글 [답변]: ASP.Net Core 를 안드로이드에서 작동하는게 가능한가요?
5014최홍준7/16/20187411Credential Provider Child Window 띄우기 [1]
5013Soul...7/16/20188031HtmlElement 스크롤 문의 [2]
5012황윤하7/13/20188580SMTP 예외처리 관련 질문 [1]
5011guest7/9/201816099c#에서 c++ DllImport 문의 입니다. [1]파일 다운로드2
5010C# 꿈...7/8/20188712C# WebService 에서 질문이 있습니다. [1]
5009C#초보자7/5/20189997[C#] 감시 프로그램을 제작했는데, 작동은 하나 폼이 멈춰버렵니다... [1]파일 다운로드1
5008도와주세요7/5/20189620 리스트 아이템의 타입을 추출해서 다시 재활용하고 싶어요.2 [6]파일 다운로드1
5007도와주세요7/4/201810730리스트 아이템의 타입을 추출해서 다시 재활용하고 싶어요. [1]
5006정환나라7/4/20189754소켓 통신과 쓰레드에 관해 질문드립니다 [4]
5005psh7/4/20188654인용에 관련해 문의 드립니다 [1]파일 다운로드2
5004까오리7/4/20188741iis8.0에서 닷넷1.1을 사용하기 위한 질문입니다. [1]
5002멋쟁이7/2/20189018WPFApp에 관한 초보 질문입니다. [1]파일 다운로드1
5001김학완7/1/20189201시작하세요 C# 7.1 프로그래밍 P53쪽 질문입니다. [2]
5000레몬6/26/201810873안녕하세요 성태님 도움으로 C# 네이버 카페 스팸글 작성되면 삭제되는 프로그램을 만들었는데요..여쭤볼게 하나 있습니다. [3]
4999jt6/21/20188748검색어 입력, 엔터 > 페북 로그인창으로 이동합니다. [4]
4998개발희망6/11/20189662C# 형변환 질문있습니다! [1]
4997초보개발자6/5/201821919C++ dll C#에서 사용하는데 보호된 메모리 오류떠요 한번만 도와주세요ㅜ [1]파일 다운로드1
4996swc6/5/201810153현업에서 주로 사용하는 DB업데이트 방법 질문입니다. [1]파일 다운로드1
4993학생6/3/20188342질문 드립니다 [1]
4991최진안5/24/20188320Credential Provider 질문 [2]
4990heyg...5/21/20189208Sybase Adaptive Server Anywhere 6 버전에 대한 질문입니다. [9]
... 31  32  33  [34]  35  36  37  38  39  40  41  42  43  44  45  ...