I'm trying to work out what this does:

ptr  = input_string;

        dest = input_string;



        while( *ptr  &&  *ptr != '\r'  &&  *ptr != '\n' ) {



            if ( *ptr >= '0'  &&  *ptr <= '9' ) *dest++ = (char) ( (*ptr) - '0'      );

            if ( *ptr >= 'A'  &&  *ptr <= 'F' ) *dest++ = (char) ( (*ptr) - 'A' + 10 );

            if ( *ptr >= 'a'  &&  *ptr <= 'f' ) *dest++ = (char) ( (*ptr) - 'a' + 10 );



            ptr++;

	}

and

prev_byte = 0;

            ptr       = input_string;



            while ( *ptr != '\x80' ) {



                hex_val  = (char) ( ( * ptr     &  '\x0f' ) << 4 );

                hex_val |= (char) ( ( *(ptr+1)  &  '\x0f' )      );



                crc_kermit     = update_crc_kermit( crc_kermit,     hex_val            );


                prev_byte = hex_val;

                ptr      += 2;

            }

I've read a dew tutorials and such on pointers, but though I sort of understand what the output looks like, I don't understand how it works.
I'm trying to do a java port and want to know what is going on.

Recommended Answers

All 2 Replies

The 1st code fragment converts hex digit chars (0123456789ABCDEF or ...abcdef) into its binary values. For example: '0' => '\0'(0), ... '9' => '\x9'(9), 'A' => '\xA'(10) and so on. It converts all chars of the C string until end of line or end of string (zero byte) occured.
It's not so good code: you need add else after 1st and 2nd if's in the loop (no sense to compare source byte with 'A' if previously it was equal to '0', for example). All other chars are skipped. This loop packs result bytes in place, from the start of source line (initially ptr == dest).
So, for example:

"s2f\r\n" => "\02\x0F\r\n"

The 2nd code fragment packs two hexadecimal byte values into one byte. For example, in "...\1\1.." string \1\1 pair is converted to (char)17 byte. Now update_crc_kermit function accepts this byte as the 2nd arg. Do it untill the next byte is not in ASCII range (0-127). It's a very dangerous loop because of special break condition. It produces wrong results if input string contains not only chars from the range 0..16. What's a questionable code fragment...

the original (full) source is here
I appreciate the help. I've got the rest ported, I was just having problems with that part. still new at bitwise operators.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.