Solved

int to binary char assignment

Posted on 2010-09-23
20
411 Views
Last Modified: 2012-05-10
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);
    }

}
0
Comment
Question by:Ph0en1x3
  • 7
  • 7
  • 5
  • +1
20 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 33747214
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33747245
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
 
LVL 2

Author Comment

by:Ph0en1x3
ID: 33747294
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
 
LVL 4

Expert Comment

by:thepanch
ID: 33747392
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 25 total points
ID: 33747481
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33747504
Or, probably better make that :

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

Expert Comment

by:rwj04
ID: 33751470
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
 
LVL 5

Assisted Solution

by:rwj04
rwj04 earned 20 total points
ID: 33751482
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 33751508
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.


Ph0en1x3 : do you have any more questions about this ?
0
 
LVL 5

Expert Comment

by:rwj04
ID: 33751529
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.  

we do need a bit more info from phoenix.

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Expert Comment

by:rwj04
ID: 33751545
>> 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
 
LVL 5

Expert Comment

by:rwj04
ID: 33751565
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
 
LVL 2

Author Comment

by:Ph0en1x3
ID: 33753246
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
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 25 total points
ID: 33753320
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
 
LVL 2

Author Closing Comment

by:Ph0en1x3
ID: 33788495
The answer is appreciated AND extremely timely but the comments about it being "homework" required me to defend the question.      
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33788841
>> 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
 
LVL 2

Author Comment

by:Ph0en1x3
ID: 33788864
Your comments are noted
0
 
LVL 5

Expert Comment

by:rwj04
ID: 33789318
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
 
LVL 2

Author Comment

by:Ph0en1x3
ID: 33789420
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
 
LVL 5

Expert Comment

by:rwj04
ID: 33789506
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now