CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Binary conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
manisha



Joined: 03 Feb 2012
Posts: 29

View user's profile Send private message

Binary conversion
PostPosted: Mon May 21, 2012 6:52 am     Reply with quote

hello everyone....
I have a small problem in hex to binary conversion.....

I have used itoa function and got binary string which is printed as correct value but the problem is in extracting the specific bits from the binary string...

We can extract by either shift operation or ANDing with 0x01 for 0th bit, 0x02 for 1st bit.... and so on. But the value which I am getting is always zero for any binary number.. Even if I use bit_test function it is always read as 0...

If anyone has a solution for this please guide me....
temtronic



Joined: 01 Jul 2010
Posts: 9135
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon May 21, 2012 6:56 am     Reply with quote

PIC101...

please show us your code as there are dozens of solutions
Ttelmah



Joined: 11 Mar 2010
Posts: 19264

View user's profile Send private message

PostPosted: Mon May 21, 2012 7:12 am     Reply with quote

It is not at all clear from your question, what your problem really is. Possibly a misunderstanding of 'what' types actually are?.

If you are using 'itoa', then you are starting _with_ a binary number, and turning it into a 'string' (an ASCII representation of this number). It is the original binary number on which you could use 'bit_test' to get the bits.

Code:


    int fred;
    char str[10];
    int1 abit;
    int8 len;

    fred = 10; //The variable 'fred' now contains the binary value b00001010
    //Remember variables are stored in binary inside the processor

    itoa(fred,2,str);
    //The array 'str', now contains the ASCII characters representing fred
    //_not_ a binary value, but the string representation of this value
 
    abit=bit_test(fred,1);

    //abit now contains '1' the second binary bit of fred.

    len=strlen(str); //work out how long the string is -> 4bytes in this case
    //1010

    if (str[len-2]=='1')
        abit=1;
    else
        abit=0;

    //Do the same working from the 'string'. Now you have to find how long
    //the string is, and work 'back' from there, doing text comparisons


From the string, you have to perform 'text' comparisons to find the bit value. Conversely from the original binary number you can just use bit tests.

I suspect you are getting confused about the difference between a binary number, and a string.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group