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

1  2  3  4  5  6  7  8  9  [10]  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
5704따봉이8/4/20224597EventHandler 관련 [1]
5703조민준8/3/20224030안녕하세요 정적 멤버 초기화 관련 간단한 질문 [2]
5702석우8/3/20223927C# WPF Ribbon IsMinimized 프로퍼티 변경 감지 방법문의 [2]파일 다운로드1
5701김기헌8/1/20224159foreach 문으로 컬렉션을 열거할 때 궁금한 점 [2]
5699갓경섭7/29/20224558C# 프로그램 느려짐 현상 [2]
5698kss7/29/20225308ValueTask 질문입니다. [5]
5697스팸7/28/20225387윈도우서버 Port 고갈문제 (Event ID 4227, 4231) [3]파일 다운로드1
5696유석7/27/20225615닷넷 4.X 와 닷넷 6.0 버전 차이 [6]
5695초보코더7/26/20224940[WCF] EndpointNotFoundException 기본 연결이 닫혔습니다. 원격 서버에 연결할 수 없습니다. [1]
5693차가워7/22/20224379socket 종료 시 reveive수신부에서 에러 나는거 처리 문의 [5]
5692도비7/21/20223961Ubuntu LVM 확장 관련 [1]파일 다운로드1
5691따봉이7/21/20224647Task 비동기 UI 크로스 쓰레딩 Err 관련 [1]
5690훈이7/20/20223912wpf질문입니다 [2]
5689질문7/20/20223697비동기 함수를 사용하는 방법에 대한 질문입니다 [1]
5688김정현7/11/20223790소스 디버깅(F5)시 프로그램 켜지지 않고 디버깅 가능한 상태 [1]
5687규철7/6/20223912개발업무 문서 관리 방법 문의 [3]
5686권용완7/5/20223883Linq Entites 에서 string[]에 담긴 데이터를 제외하는 방법 문의 [3]
5685차가워7/4/20224052메모리 선택 문의 [1]
5683김민혁7/1/20224598HttpWebRequest POST 전송 관련해서 질문 드립니다. [4]파일 다운로드1
5682김기헌6/20/20224042안녕하세요 서로 다른 스레드에서 동일 인스턴스의 메소드를 호출할때 질문드립니다 [2]
5681미래는투...6/19/20223933VC++ 빌드 관련 문의 (h파일이 바뀌었는데 컴파일을 꼭 하지 않아도 되나요?) [1]
5680Syong6/12/20224923EXE파일에 속성값 추가하는 방법이 있나요? [2]파일 다운로드1
5679차가워6/8/20224117tcpip socket 여러 랜카드 사용 시 연결 문의 [1]
5678차가워6/7/20224023원자적 실행 문의 [3]
5677장성욱6/7/20224047블루투스 포트와 시리얼포트 연결시 에러 [2]
5675차가워5/29/20224735윈도우설정 프로세스사용계획 옵션과 c# 스레드 [1]
1  2  3  4  5  6  7  8  9  [10]  11  12  13  14  15  ...