Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11233
글쓴 사람
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)
4909필승11/5/201710604TextBox에 관해 질문 드립니다. [5]
4908필승11/1/201713945특정 시각에 프로그램 종료 또는 재시작시 오류 발생 [8]
4907심너울10/29/20179536선생님이 쓰신 "시작하세요! C# 7.1~"으로 공부하고 있는데요~ [2]
4906guest10/25/20179100.net core, .net standard 관련해서 궁금합니다 [2]
490510/25/20178375watermark 관련문의 [1]
4903Ho10/25/20178772간단한 서버를 구현해 보고 싶습니다. [2]
4902황재승10/22/20179808C# 7.1 프로그래밍 왕초보자도 이해할 수 있는 수준인가요? [1]
4901김레오10/17/201714511c# 프로그램 난독화 도구인 confuserex라는 도구를 사용하다 여쭙습니다. [2]
4900황준범10/12/201710283ClickOnce 배포관련 질문드립니다. [1]
4899Ques...9/28/201712719C# 프로그램이 "응답 없음" 시에도 계속 독립적으로 돌아가는 타이머 생성법 [3]
4898ssdrm9/27/20179500Clickonce 실행시 보안에 막힙니다 ㅠ [1]
4897김태진9/23/20179677윈도우7 작업관리자의 상세(details)탭 생성에 대해 여쭙습니다. [1]
4896장진국9/19/201711026안녕하세요 WPF 에서 Window객체가 가비지 콜렉션에 의해 수집되지 않는거 같아서 문의드립니다. [1]
4895Ques...9/18/201710693Generic 에 관하여 질문드립니다. [5]
4894얼마전6...9/14/201712595C# 7.1에서 보강된 부분만 PDF로 제공하는 건 아니되나요? [2]
4893BigII9/14/201711622타 언어(JAVA, PHP 등)에서 받은 RSA 개인키 문자열을 이용하여 내용 복호화 가능 여부 [4]
4892Ques...9/13/201710636서브 폼에서는 무거운 작업을해도 속도가 빠를까요 ?? [1]
4891윤현수9/11/201711283Taskbar에 관한 질문입니다. [5]
4890제발9/11/20179818 시작하세요! C# 6.0 프로그래밍 책이 절판됐나요? [5]파일 다운로드1
4889낙낙이9/7/20179237안녕하세요! xsl관련 이야기입니다. [1]
4888heyh...9/6/20179070클릭원스를 수동으로 배포 시 업데이트 할 때 [1]
4887이경현9/4/20179625Windows server 2012 파일 없어짐 현상... [1]
4886질문자9/1/201711506disconnecteditem에 대하여 아시나요? [4]파일 다운로드1
4883솔솔8/30/20179999dataview에서 select한 index 가져오기 [1]
4882user8/30/201712637UI 변경 작업 여러개를 동시에 사용하려면 어떻게 해야되나요 ?? [7]
4884user8/31/20179295    답변글 [답변]: UI 변경 작업 여러개를 동시에 사용하려면 어떻게 해야되나요 ??파일 다운로드1
... 31  32  33  34  35  36  37  [38]  39  40  41  42  43  44  45  ...