Thread: C to Java

  1. #1
    Registered User
    Join Date
    Sep 2006

    C to Java

    I was just wondering if you guys would kindly help me out converting the following code to java. I tried it myself but I am not very good at C or C++. trying to learn it.
    Thank you.

    ---Code Starts ---
    typedef unsigned char byte;
    typedef unsigned int uint;
    static byte decode_hex[257]={0};
    #define hex_none 0x10
    #define hex_eofc 0x20
    #define sgetc(s) (*((s)++))
    sreadhex(str, rlen, nread, odd_digit, s)
        byte *str;
        uint rlen;
        uint *nread;
        int *odd_digit;
        register byte *s;
            byte *ptr=str;
            byte *limit = ptr+rlen;
            byte val1 = (byte)*odd_digit;
            byte val2;
            register char *decoder = (char*)(decode_hex+1);          
            if (decoder[-1]==0)
                static char hex_chars[]= "0123456789ABCDEFabcdef";
                int i;
                memset(decoder-1, hex_none, 257);
                for (i=0; i<16+6; i++)
                decoder[hex_chars[i]] = (i>=16? i-6: i);
                decoder[0] = hex_eofc; 
            if (val1<=0xf) goto d2;
    d1:   while ((val1=decoder[sgetc(s)])>0xf)
    { if (val1==hex_eofc) {*odd_digit=-1; goto ended;}
    d2:   while ((val2=decoder[sgetc(s)])>0xf)
    { if (val2==hex_eofc) {*odd_digit=val1; goto ended;}
            *ptr++ = (val1<<4) +val2;
            if (ptr<limit) goto d1;
            *nread = rlen;
            return 0;
    ended: *nread = ptr - str;
    return 1;
    Last edited by Salem; 09-26-2006 at 03:45 PM. Reason: Added [code][/code] tags, use them yourself

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Portland, Or
    Sorry you didn't read the FAQs and if you don't feel like putting forth that small bit of effort then why should we help you?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Maybe start with a proper description of what the code actually does, then write the Java version from scratch.

    That code looks like it's about 20 years old. The function is written in K&R style, and is riddled with gotos and non-standard array hackery. Definitely looks like something tuned for performance rather than readability.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    FWIW ...

    sreadhex is a function that takes a string of characters that represent hexadecimal digits. Each character in the string represents a hexadecimal number. All such numbers can be represented in 4 bits:

    '3' -> 3 -> 0011
    '4' -> 4 -> 0100
    'A' -> 10 -> 1010
    'B' -> 11 -> 1011

    Thus the values of two characters can be packed into a single byte, and a whole string can be packed into about half its length.

    Example 1: "34AB" is packed into:
    Byte 1: 00110100 Byte 2: 10101011

    Example 2: There is an odd number of characters - "34ABF"
    Byte 1: 00110100 Byte 2: 10101011

    'F' (0xf) as the extra return value, which is typically used in the next call to sreadhex.

    Example 3: sreadhex completely ignores characters in the source string that do not represent hexadecimal digits.
    "34AB", "3 4 A B", "3G4GAMB-" will yield the same result.

    sreadhex(str, rlen, nread, odd_digit, s)


    str is a byte array to fill. It is the destination.

    rlen is the maximum number of bytes to be filled in str.
    nread is a pointer to an integer. When the routine finishes, it leaves the number of bytes it used in that integer.

    odd_digit is a pointer to an integer. The caller uses it to pass in the first digit (if the last call used an odd number of digits; the routine leaves either -1 or the extra digit in it after return. (The ‘odd’ in odd_digit refers to its being set when there are an odd number of hexadecimal characters to be placed in str)

    s is a null-terminated string. It is the source of hexadecimal characters.

    sreadhex returns 0 or 1.

    1 means sreadhex ran out of characters before filling str.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Thnx guys, I converted it.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java for real-time applications
    By zacs7 in forum A Brief History of
    Replies: 4
    Last Post: 08-26-2008, 06:34 AM
  2. C#, Java, C++
    By incognito in forum A Brief History of
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  3. The Java language is being expanded
    By DavidP in forum A Brief History of
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  4. Java woes
    By DavidP in forum A Brief History of
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  5. How to use Java with C++
    By Arrow Mk 84 in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2003, 04:12 PM