In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

I need to take a negative integer, and represent it as a two's complement, for instance -15?? I wrote this part of the code but how do I take the number and put it into a char other then a table? -15 (base 10) int = F1 (base 16) char

void cooling_convert_dec2bin(int number)

{

int x, y;

x = y = 0;

for(y = 7; y >= 0; y--)

{

x = number / (1 << y);

number = number - x * (1 << y);

}

}

void cooling_convert_dec2bin(in

{

int x, y;

x = y = 0;

for(y = 7; y >= 0; y--)

{

x = number / (1 << y);

number = number - x * (1 << y);

}

}

If you store the value -15 in an int, then that's its value. You can then represent that value in whatever base you want (binary, octal, decimal, hexadecimal), but that doesn't change the value.

In other words : converting decimal to binary when the input and output are an int, has no meaning. The resulting int will be exactly the same as the original int.

10(base 2)= 2 (base 10)

10(base 10) = 1010(base 2)

10(base 10)=A (base 16)

10(base 10)= 12(base 8)

this is correct?

you probably have to perform the two's complement. here's the easiest way i can think of at the moment, without using sprintf(). be aware scanf() will behave very badly if you don't enter a valid input.

int number = -1;

while (number < 0 || number > 127)

{

printf("enter a positive number less than 128 : ");

scanf("%d", &number);

}

printf("converting %d to negative...\n", number);

number ^= -1; // XOR the number with -1 (i.e. 0xFFFFFFFF...) to "flip bits"

number += 1; // add +1, to complete the 2's complement

printf("result is 0x%02X hex (%d decimal)\n");

while (number < 0 || number > 127)

{

printf("enter a positive number less than 128 : ");

scanf("%d", &number);

}

printf("converting %d to negative...\n", number);

number ^= -1; // XOR the number with -1 (i.e. 0xFFFFFFFF...) to "flip bits"

number += 1; // add +1, to complete the 2's complement

printf("result is 0x%02X hex (%d decimal)\n");

i think this should work. let me know if it doesn't, it's late and i don't feel like testing it.

printf("result is 0x%02X hex (%d decimal)\n", number, number);

I'm also mystified why sprintf wouldn't be allowed. The point was to put the result in a char string, so you need to print to a string. You use %02x too, but you used printf instead of sprintf. I fail to see the difference heh.

Ph0en1x3 : do you have any more questions about this ?

if i'm right about it, my code may not even be acceptable. as you pointed out, i'm still using the printf() specifiers.

we do need a bit more info from phoenix.

eh, yeah, you're probably right. i assumed you knew it was homework. anyhow, at this level, i don't see how it matters. at some point you basically just have to explain it by doing it.

theres still some work to do.

Send the set temperature of -1.5 degrees C

1. The controller command, CC is by defination 1c.

2. Multiply the desired set-point temperature by 10 (base 10) (-1.5 x 10 = -15).

3. Covert -15 (base 10) to hexadecimal by taking the two's complement of the number. (This can be easily done by using the formula (2 ^ n) - N, where n is the word-bit size and N is the absolute value of the number being converted.) Hence, you have (2 ^ 16) - 15 = 65521 (base 10). This value then converts to fff1 (base 16).

ASCII char Hex value

1 31

c 63

f 66

f 66

f 66

1 31

I was able to figure out how to convert it to binary then got stuck and decided to call out for help. I haven't tried the recommendations yet but thought I would clarify the question of if this is a homework assignment...

From the example, it looks like it's 16bit, rather than 8bit, so what I suggested earlier would become :

char str[5] = "";

sprintf(str, "%04x", ((unsigned int) (0x10000 + value)) & 0xFFFF);

Note that this allows any value between -32768 and 32767 to be converted correctly, on any standards compliant platform.

If you cannot (or don't want to) use sprintf, then simply store the resulting unsigned int value in a variable, and print each hex character manually.

The basis for the grade should be the quality of the answer.

Whether or not you were asked by someone if this is homework or not, does not have any impact on the validity of the answer afaik. Correct me if I'm wrong :)

Note that this site forbids academic dishonesty, so if an expert has a doubt about whether or not a question is homework, he is right to voice that concern, and clarify the issue. There is no reason to feel offended by that - it is just the expert trying to do the right thing. If it's not homework, you can simply state so (like you did), and continue as usual.

you can't hardly help giving away the answer; it's such a fundamental concept, you might as well just give them the basic procedures to solve it, and let them figure out how to incorporate it into the larger program.

in any event, i dont care that he gave it a "B". the points are trivial, and the answers given here aren't any better than the 100 other solutions that can be found with a Google search.

The instructors know when a student has grasped the subject. We were encouraged to see beyond the homework test answers to judge the students level of comprehension and understanding for the subject matter. I used this soft skill to move an 89 to a 90 several times.

My project lead encouraged me to try this forum and I do appreciate the assistance.

sorry for assuming, i should have asked more politely rather than jumping to an immediate conclusion. in hindsight, i should have considered that it might not be a HW question by the function name in your code.

regards

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

The easiest way would probably be something like this :

char str[3] = "";

sprintf(str, "%02x", (0x100 + value) & 0xFF);