MeanwhileInHell
MeanwhileInHell

Reputation: 7053

C equivalent of the Java "Character.digit(hexText.charAt(i), 16)"

I am trying to decode a hex string back to binary data using C. I have done this just dandy in Java, and have the equivalent encode function in C, but can't quite get the decode working. Behold...

Java code:

private static String encodeToHex(byte[] bytes) {
    StringBuilder stringBuilder = new StringBuilder();
    for (byte b : bytes) {
        stringBuilder.append(String.format("%02x", b & 0xff));
    }
    return stringBuilder.toString();
}

private static byte[] decodeFromHex(String hexText) {
    int length = hexText.length();
    byte[] data = new byte[length / 2];
    for (int i = 0; i < length; i += 2) {
        data[i / 2] = (byte) ((Character.digit(hexText.charAt(i), 16) << 4) + Character.digit(hexText.charAt(i + 1), 16));
    }
    return data;
}

C code:

void encodeToHex(const unsigned char *encryptedText, const size_t length, char *hexEncodedText) {
    for (int i = 0; i < length; i++) {
        if (i == 0) {
            sprintf(hexEncodedText, "%02x", encryptedText[i] & 0xff);
        } else {
            sprintf(hexEncodedText + strlen(hexEncodedText), "%02x", encryptedText[i] & 0xff);
        }
    }
}

// The poor attempt. Note, I do not write C like a native
void hexDecode(char *hexEncodedText, unsigned char *decodedCipherText) {
    int length = strlen(hexEncodedText);
    unsigned char data[length / 2];
    for (int i = 0; i < length; i += 2) {
        data[i / 2] = (char) ((hexEncodedText[i] << 4) + (hexEncodedText[i + 1]));
    }
    memcpy(decodedCipherText, data, length / 2);
}

I guess what I am trying to look for is the C equivalent of the Java Character.digit(hexText.charAt(i), 16). Anyone any ideas on how to do this?

Thanks in advance.

Upvotes: 1

Views: 1226

Answers (3)

Lee Meador
Lee Meador

Reputation: 12985

In C you can fool with the ASCII of the characters.

int hexDigit(char digit) {
    int val = digit - '0';
    if (digit > 'a') { // leave this out if you know its upper case
        val -= 'a' - 'A';
    }
    if (val > '9') {
        val -= "A" - '9' + 1;
    }
    return val;
}

Upvotes: 2

PoByBolek
PoByBolek

Reputation: 3915

You need to convert the hex characters to their actual integer value. You can do this by subtracting the hex character from char literals like so:

unsigned char valueOfHexCharacter(char hex) {
    if (hex >= '0' && hex <= '9')
        return hex - '0';
    else if (hex >= 'a' && hex <= 'f')
        return 10 + (hex - 'a');
    else if (hex >= 'A' && hex <= 'F')
        return 10 + (hex - 'A');
    else
        return -1; // invalid hex character
}

You can than use it in your code

data[i / 2] = (valueOfHexCharacter(hexEncodedText[i]) << 4) + valueOfHexCharacter(hexEncodedText[i + 1]);

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

C does not have a direct equivalent of Character.digit, but since your radix is hard-coded to 16, you can write one yourself:

int hexDigit(char digit) {
    static const char *hexDigits = "0123456789ABCDEF";
    return strchr(hexDigits, toupper(digit)) - hexDigits;
}

Upvotes: 5

Related Questions