Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > initializing a number in binary form

Reply
Thread Tools

initializing a number in binary form

 
 
tguclu
Guest
Posts: n/a
 
      07-25-2007
Hi
I'm trying to make a CRC calculator and for test purposes i'm writing
some test routines.
I have the pre-calculated crc values for some binary numbers and i
want to use them in my code.
Is it possible to initialize an integer in binary form ? For example
like this?

int CrcHesapla()
{
int reg = 0x00;
int msg = b'1101011011';

}

actually it didn't compile.Any help ?

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-25-2007
tguclu wrote:

> Hi
> I'm trying to make a CRC calculator and for test purposes i'm writing
> some test routines.
> I have the pre-calculated crc values for some binary numbers and i
> want to use them in my code.
> Is it possible to initialize an integer in binary form ? For example
> like this?
>
> int CrcHesapla()
> {
> int reg = 0x00;
> int msg = b'1101011011';
>
> }
>
> actually it didn't compile.Any help ?


No. But you can use hexadecimal constants instead. Prefix them with an '0x'
or '0X'.

 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      07-25-2007
tguclu wrote:
> Hi
> I'm trying to make a CRC calculator and for test purposes i'm writing
> some test routines.
> I have the pre-calculated crc values for some binary numbers and i
> want to use them in my code.
> Is it possible to initialize an integer in binary form ?


What does your C reference book say about initialising integers? What
does the standard (which is available online) say about initialising
integers? Does the C FAQ (at c-faq.com) say anything about initialising
integers? Or didn't you look?

The short answer is that there is no such syntax. You can initialise
integers in decimal, octal(one "digit" for 3 bits) or hexadecimal (one
"digit" for 4 bits) formats.

If you feel you must express your numbers in binary form, write a little
function to take a string of the form "10110111" or whatever and return
an integer.
 
Reply With Quote
 
Spoon
Guest
Posts: n/a
 
      07-25-2007
tguclu wrote:

> I'm trying to make a CRC calculator and for test purposes i'm writing
> some test routines.
> I have the pre-calculated crc values for some binary numbers and i
> want to use them in my code.
> Is it possible to initialize an integer in binary form ? For example
> like this?
>
> int CrcHesapla()
> {
> int reg = 0x00;
> int msg = b'1101011011';
>
> }


http://groups.google.com/group/comp....f?dmode=source

Newsgroups: comp.lang.c,comp.arch.embedded
Subject: Binary constant macros
Date: 26 Feb 2004 07:36:35 -0800
Message-ID: < >
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      07-25-2007
Mark Bluemel wrote:
> tguclu wrote:
>
>> Hi
>> I'm trying to make a CRC calculator and for test purposes i'm writing
>> some test routines.
>> I have the pre-calculated crc values for some binary numbers and i
>> want to use them in my code.
>> Is it possible to initialize an integer in binary form ?

>
> If you feel you must express your numbers in binary form, write a little
> function to take a string of the form "10110111" or whatever and return
> an integer.


Or use strtoul().


--
Thad
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      07-25-2007
tguclu wrote:
>
> Hi
> I'm trying to make a CRC calculator and for test purposes i'm writing
> some test routines.
> I have the pre-calculated crc values for some binary numbers and i
> want to use them in my code.
> Is it possible to initialize an integer in binary form ? For example
> like this?
>
> int CrcHesapla()
> {
> int reg = 0x00;
> int msg = b'1101011011';
>
> }
>
> actually it didn't compile.Any help ?


Learn Hex!

b '11 0101 1011';
0x 3 5 b

int msg = 0x35b;

--
pete
 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      07-26-2007
pete wrote:
> tguclu wrote:
>> Hi
>> I'm trying to make a CRC calculator and for test purposes i'm writing
>> some test routines.
>> I have the pre-calculated crc values for some binary numbers and i
>> want to use them in my code.
>> Is it possible to initialize an integer in binary form ? For example
>> like this?
>>
>> int CrcHesapla()
>> {
>> int reg = 0x00;
>> int msg = b'1101011011';
>>
>> }
>>
>> actually it didn't compile.Any help ?

>
> Learn Hex!
>
> b '11 0101 1011';
> 0x 3 5 b
>
> int msg = 0x35b;
>

Go pete. Finally the right answer.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
tguclu
Guest
Posts: n/a
 
      07-26-2007
On Jul 26, 12:54 am, pete <pfil...@mindspring.com> wrote:
> tguclu wrote:
>
> > Hi
> > I'm trying to make a CRC calculator and for test purposes i'm writing
> > some test routines.
> > I have the pre-calculated crc values for some binary numbers and i
> > want to use them in my code.
> > Is it possible to initialize an integer in binary form ? For example
> > like this?

>
> > int CrcHesapla()
> > {
> > int reg = 0x00;
> > int msg = b'1101011011';

>
> > }

>
> > actually it didn't compile.Any help ?

>
> Learn Hex!
>
> b '11 0101 1011';
> 0x 3 5 b
>
> int msg = 0x35b;
>
> --
> pete


thx..i will note it down..
what about if you have lots of binary numbers with varying bit
numbers in a text file ?
Will you align them in nibles or woudly you prefer to do some trick ?
Like finding a way out to assing them to some variable by copy-paste?
or improve your hex counting skills..

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-26-2007
tguclu wrote:

> On Jul 26, 12:54 am, pete <pfil...@mindspring.com> wrote:
>> tguclu wrote:
>>
>> > Hi
>> > I'm trying to make a CRC calculator and for test purposes i'm writing
>> > some test routines.
>> > I have the pre-calculated crc values for some binary numbers and i
>> > want to use them in my code.
>> > Is it possible to initialize an integer in binary form ? For example
>> > like this?

>>
>> > int CrcHesapla()
>> > {
>> > int reg = 0x00;
>> > int msg = b'1101011011';

>>
>> > }

>>
>> > actually it didn't compile.Any help ?

>>
>> Learn Hex!
>>
>> b '11 0101 1011';
>> 0x 3 5 b
>>
>> int msg = 0x35b;


> what about if you have lots of binary numbers with varying bit
> numbers in a text file ?
> Will you align them in nibles or woudly you prefer to do some trick ?
> Like finding a way out to assing them to some variable by copy-paste?
> or improve your hex counting skills..


It depends on the requirement of the program and the format of the binary
values. If you need to read them as byte values, then that's the correct
method. If they are meant to be read as a series of word values, then
that's the correct method. (Note - "word" is not defined in Standard C, but
it's a pretty common term in computing.)

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-27-2007
tguclu <> writes:

> On Jul 26, 12:54 am, pete <pfil...@mindspring.com> wrote:
>> tguclu wrote:
>>
>> > Is it possible to initialize an integer in binary form ? For example
>> > like this?

>>
>> > int CrcHesapla()
>> > {
>> > int reg = 0x00;
>> > int msg = b'1101011011';
>> > }

>>
>> Learn Hex!
>>
>> b '11 0101 1011';
>> 0x 3 5 b
>>
>> int msg = 0x35b;


> thx..i will note it down..
> what about if you have lots of binary numbers with varying bit
> numbers in a text file ?
> Will you align them in nibles or woudly you prefer to do some trick
> ?


<OT>If the conversion was a one-off, I would use Perl. No more than
one line of code if the input file is relatively clean.</OT>

If you need to get the values into your program *at runtime* use the
library function strtoul -- it can convert using base 2.

Similarly, a few lines of standard C is all you'd need to write a
converter to turn the file of numbers in base two[1] to hexadecimal.

[1] I say 'in base two' because a 'file of binary numbers' is
ambiguous. The input seems to be a text file with numbers in base
two.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion of real number into binary number rajeswari01 Hardware 1 07-15-2011 05:30 AM
conversion of real number into binary number rajeswari01 Hardware 0 10-15-2010 12:30 AM
How do you convert a hexadecimal number to a binary number? Bob Sanders Ruby 5 05-30-2008 11:50 AM
Initializing the number of slots in a dictionary Jon Smirl Python 5 08-07-2006 07:22 PM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57