• C

# int to binary char assignment

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);
}

}
LVL 2
###### Who is Participating?

Commented:
That's not exactly what the title suggested, but that clarified it well.

The easiest way would probably be something like this :

char str[3] = "";
sprintf(str, "%02x", (0x100 + value) & 0xFF);
0

Commented:
I don't understand what you are trying to do.

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.
0

Commented:
Could you give a bit more detail about what you're trying to do exactly. It would be good if you could give an illustrative example ...
0

College InstructorAuthor Commented:
Convert -15 (base 10) to hexadecimal by taking the two's compliment of the number.    This needs to be converted to F1 (base 16).    I need to store it in a char array[2].    Does this help?
0

Commented:
you are trying to conver from different bases?  for example

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?
0

Commented:
Or, probably better make that :

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

Commented:
this looks like a homework assignment.   these types of  homework assignment typically do not allow such methods to get the "easy answer" with sprintf()

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");

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

0

Commented:
bah, i wish there were an 'edit' feature.    the last line of the code snippet should be:

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

0

Commented:
rwj04 : I did not pick up on whether this was homework or not (could you clarify this for us, Ph0en1x3 ?). But if you believe it's homework, you should not have posted a full code solution.

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.

0

Commented:
i've seen a lot of homework questions posted on other forums.    this looks like a typical example.    these homework problems are usually very strict about what you can/can't use.

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

0

Commented:
>> But if you believe it's homework, you should not have posted a full code solution.

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.

0

Commented:
anyhow, the OP still has to figure out how to take my example snippet and incorporate it into the function, and correctly pass the value back to the caller.

theres still some work to do.

0

College InstructorAuthor Commented:
No it is NOT homework, I am an embedded software geek and I am taking the  example from the TEC controller technical manual.   I would send you a image of the manual but it has so many handwritten notes on it that it is better if I type the example  from pg 49

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...
0

Commented:
Thank you for clarifying.

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.
0

College InstructorAuthor Commented:
The answer is appreciated AND extremely timely but the comments about it being "homework" required me to defend the question.
0

Commented:
>> but the comments about it being "homework" required me to defend the question.

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.
0

College InstructorAuthor Commented:
0

Commented:
i had a real reason to think it was a homework question:   it's typical CSC 101 homework material and  i've seen 100 variants of this question asked in various help forums, all of them have been by students working on homework

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.

0

College InstructorAuthor Commented:
I agree, as a college professor for 6+ years, even if you give them the answer they won't know what to do with it unless they have the "knack"...

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.
0

Commented:
glad it helped.   hope you come back with future questions.   there's a lot of experts here will really deep subject knowledge.

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
0
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.