Hi, i am working on a project that involves converting 8-bit hex numbers into integer.
Firt, I have to say that my hex number is not a number right now, it is only a bunch of characters.

for example:

int d;
char code[9];
code="FFFFFF01";
d=strtol(code,NULL,16);

when I try to printf intger d, it never gives me negative numbers.

Anyone has any idea?
Thanks in advance.
Fatih

Assuming your ints are 32-bit, FFFFFF01 exceeds INT_MAX. strtol should correctly return INT_MAX because the value passed in is out of range. While I'm sure you expected strtol to take a value with the sign bit set and produce a negative value, that's not how it works. The values are expected to be in range, and any sign is interpreted with a leading + or -.

Assuming your ints are 32-bit, FFFFFF01 exceeds INT_MAX. strtol should correctly return INT_MAX because the value passed in is out of range. While I'm sure you expected strtol to take a value with the sign bit set and produce a negative value, that's not how it works. The values are expected to be in range, and any sign is interpreted with a leading + or -.

ok. Let me explain my problem. We are writing an assembler in our project and accordingly produce the machine codes.

for example : add -1

add's instruction code is 03 and -1 is FFFFFF and combine them together and produce machine codes. I did that by only string processing. So, I produced FFFFFF03 as a machine code.

We are supposed to write these machine codes into binary file. But it never goes right.
I am using fwrite() function.

Cheers.
Fatih

I'd probably choose to parse and build values manually:

#include <stdio.h>
#include <ctype.h>

int hex_value(char c)
{
    const char *digits = "0123456789ABCDEF";
    int i;

    for (i = 0; digits[i] != '\0'; i++) {
        if (toupper(c) == digits[i])
            break;
    }

    return i;
}

int main()
{
    char *code = "abcdef012345";

    while (*code != '\0') {
        char byte = 0;

        byte |= hex_value(code[0]) << 4;
        byte |= hex_value(code[1]);

        printf("%#x ", byte);

        code += 2; // Jump by one byte
    }

    return 0;
}

Note that there's no error checking in the above code.

I am a kind of beginner for C. Please do not annoy me. I have my hexadecimal numbers represented by strings, like ffffff01, ok.

I have to convert this into signed integers. The code worked on my computer, but it is not what I want.

Thanks.

I am a kind of beginner for C. Please do not annoy me.

It's not often that I literally laugh out loud. "Please do not annoy me" is a hoot. :)

I have my hexadecimal numbers represented by strings, like ffffff01, ok.

Yes, I understand.

I have to convert this into signed integers.

Does this work for you, O annoyed one?

#include <stdio.h>
#include <ctype.h>

int hex_value(char c)
{
    const char *digits = "0123456789ABCDEF";
    int i;

    for (i = 0; digits[i] != '\0'; i++) {
        if (toupper(c) == digits[i])
            break;
    }

    return i;
}

int main()
{
    char *code = "ffffff01";
    int code_bytes = 0;

    while (*code != '\0') {
        code_bytes = code_bytes | hex_value(*code);

        if (*++code != '\0')
            code_bytes <<= 4;
    }

    printf("%d\n", code_bytes);

    return 0;
}

sorry for 'do not annoy me'. since i am an ESL, I did not mean that. I meant 'I dont want to annoy you':)

Fatih

I have to thank you and demand a forgiveness by being inconvenience saying nonsense sentences.

Wherever you are, be my guest when you visit Turkey,Bursa.

Fatih.

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.