Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11130
글쓴 사람
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]

... 16  17  18  19  20  21  [22]  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
5372민성8/24/20207629안녕하세요 WPF 웹브라우저 컨트롤에서 웹브라우저 자체를 엣지로 할 수가 있나요? [2]
5371서현준8/11/20206644Hyper-V 내부 네트워크 환경 중, VM 에서 인터넷 접속 [1]
5370독자8/11/20205697.net createobject 관련 질문입니다.. [4]
5369독자8/10/20206016c# 8.0 프로그래밍 책에 대한 질문 드립니다 [1]
5368guest8/10/20207308시작하세요! C# 8.0 프로그래밍 책에 오타가 있습니다. [1]
5367박순기8/8/20206219디버깅 시 반전되는 부분 글 색상이나 배경색을 변경하는 방법 [3]파일 다운로드1
5366간절함8/7/20206909하나의 솔루션에서, 32비트 프로젝트에서 다른 64비트 프로젝트 참조하는 방법 궁금합니다. [4]
5365영귤8/6/20206160TimeSpan 구할 때 시간대를 바꿔도 같은 값이 나오는 것 같습니다. [1]
5364박순기8/5/20206206visual studio의 menu bar font size 변경방법 문의 [2]파일 다운로드1
5363클락8/1/20206830c# 9.0 출간 준비 중이신가요? [2]
5361하태7/29/20206442안녕하세요! 바이너리데이터 파싱 질문 드리겠습니다!. [2]
5360진우7/28/20207409엑셀에서 발생하는 모든 이벤트를 감지 할수 있는지 궁금합니다. [6]파일 다운로드1
5358홍길동7/22/20206152개체에서 open 속성이나 매서드가 지원 되지 않읍니다.라고 오류창이 뜨는데. [1]
5357손동섭7/22/20206260책 예제코드... [3]
5355neska7/21/20206637안녕하세요 문의 드립니다 [1]
5354민성7/15/20208111안녕하세요 db연결 문자 보안 [2]
5353형성재7/14/20207139Visual Studio Setup Project에서 VC++2010재배포패키지설치 [4]
5352윤식7/10/20207116닷넷코어 빌드 시 runtimeconfig 파일을 없앨 수 있는지.. [4]
5351runr...7/2/20206961C# 에서 C++ dll 병렬 사용 [1]
5350홍성호6/29/20208567C++에서 정의된 구조체(공유메모리)를 C#으로 공유메모리에서 구조체 변환시 에러가 발생 합니다. [1]파일 다운로드1
5349jero...6/24/20206088logon credential provider 관련 문의드립니다. [1]
5348776/19/20205833시작하세요 C# 8.0프로그래밍 책에 대한 질문입니다. [8]
5347민성6/19/20206639안녕하세요 WPF Binding에서요 [1]
5346dimo...6/18/20206683Task.WhenAll() 메소드를 이용한 코드를 좀더 짧게 사용하고 싶은데 다른 표현이 있을까요? [3]
5345질문요6/12/20205791idc vs 집 속도 [2]
5344하태6/2/20206926c++ .ilb에서 c# dll 호출 질문 드리겠습니다 (콜백함수 전달) [4]
... 16  17  18  19  20  21  [22]  23  24  25  26  27  28  29  30  ...