Microsoft MVP성태의 닷넷 이야기
c++ to C# 컨버팅 문의 [링크 복사], [링크+제목 복사]
조회: 11247
글쓴 사람
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)
5806스레드1/25/20233142총정리 - 다양한 스레드들 [초안] [1]파일 다운로드1
5805어웨이트1/25/20233003Taskcontinuewith vs Async/Await [2]파일 다운로드1
5804나이많은...1/25/20232864MS의 Dependency Injection(DI)에 AddSingleton으로 등록된 객체의 Event 등록후 사용시 앱 종료시 별도로 Event를 해지해야 하나요? [2]
5803dssc...1/24/20233100드라이브 문자를 통해서 물리 디스크 명칭을 알아내고 싶습니다. [1]
5802모바일앱1/22/20232978XAMARINE vs Android Studio [7]
5801블루투스1/19/20233709WPF 은행지폐계수기 개조 후 결과값 서버 전송 [2]파일 다운로드1
5800김민아1/19/20234044안녕하세요 가비지 컬렉터 동작 원리 중 궁금한점이 있습니다 [5]
5799guest1/19/20233190C# 공유폴더 내 Acess 디비 공유 [2]
5798kss1/19/20232968책 오탈인가요? [1]
5797이거비버1/19/20233063C# 공부 이후 MS 프레임워크.. 어느것을 공부해야할까요? 너무 많아서 정신이 없네요 [5]
5796guest1/19/20232778해킹 test [3]
5795동기1/18/20232619동기 스레드와 메서드와 While [2]
5794박규동1/18/20232586.net publish 할때마다 runtimeconfig 값이 바뀌는 현상 [1]
5793후후훗1/18/20232758.NET Core 에서 사용중인 함수 후킹 방법 [2]
5792Will...1/18/20232697소스코드 Log Write 기능을 리스트업 질문 [9]
5791PLC1/16/20233401C# - 실시간 5개 룸 모니터링 [15]
5790집으로 ...1/13/20233051[UI 멈춤 현상]deadlock 관련 글을 보고 혹시나 하고 문의 드립니다. [4]
5789guest1/10/20232949스레드와 Async Task [2]파일 다운로드1
5788kr11/10/20233402C# 에서 제공하는 컬렉션들의 차이점이 궁금합니다. [3]
5787stack1/10/20233189STACKOVERFLOW [1]
5786Dev ...1/9/20234060익명 클래스 말고 익명 구조체는 불가능한걸까요? [4]
5785음성인식1/8/20233655음성인식 System.Speech - 문법에 사용된 언어가 음성 인식기의 언어와 일치하지 않습니다. [2]
5784MS워드1/8/20233282MS워드에서 ctrl Z는 클립보드를 이용하나요? 아니면 참조자 이용하나요? [7]
5783구직자1/7/20233321C#개발자 구인광고와 초급개발자 [3]파일 다운로드1
5782Sqli...1/5/20233296윈도우11 노트북에서 exe(Sqlite)만들어 윈도우 7 PC에 설치 시 [5]
5781List맨1/5/20233794List.Add("newobj") 속도는 빠른 편인지요? [11]
1  2  3  4  5  [6]  7  8  9  10  11  12  13  14  15  ...