Connecting Tech Pros Worldwide Help | Site Map

initializing a number in binary form

tguclu
Guest
 
Posts: n/a
#1: Jul 25 '07
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 ?

santosh
Guest
 
Posts: n/a
#2: Jul 25 '07

re: initializing a number in binary form


tguclu wrote:
Quote:
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'.

Mark Bluemel
Guest
 
Posts: n/a
#3: Jul 25 '07

re: initializing a number in binary form


tguclu wrote:
Quote:
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.
Spoon
Guest
 
Posts: n/a
#4: Jul 25 '07

re: initializing a number in binary form


tguclu wrote:
Quote:
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: <b9d5cc04.0402260736.60017ce2@posting.google.com >
Thad Smith
Guest
 
Posts: n/a
#5: Jul 25 '07

re: initializing a number in binary form


Mark Bluemel wrote:
Quote:
tguclu wrote:
>
Quote:
>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
pete
Guest
 
Posts: n/a
#6: Jul 25 '07

re: initializing a number in binary form


tguclu wrote:
Quote:
>
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
Joe Wright
Guest
 
Posts: n/a
#7: Jul 26 '07

re: initializing a number in binary form


pete wrote:
Quote:
tguclu wrote:
Quote:
>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 ---
tguclu
Guest
 
Posts: n/a
#8: Jul 26 '07

re: initializing a number in binary form


On Jul 26, 12:54 am, pete <pfil...@mindspring.comwrote:
Quote:
tguclu wrote:
>
Quote:
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?
>
Quote:
int CrcHesapla()
{
int reg = 0x00;
int msg = b'1101011011';
>
Quote:
}
>
Quote:
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..

santosh
Guest
 
Posts: n/a
#9: Jul 26 '07

re: initializing a number in binary form


tguclu wrote:
Quote:
On Jul 26, 12:54 am, pete <pfil...@mindspring.comwrote:
Quote:
>tguclu wrote:
>>
Quote:
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?
>>
Quote:
int CrcHesapla()
{
int reg = 0x00;
int msg = b'1101011011';
>>
Quote:
}
>>
Quote:
actually it didn't compile.Any help ?
>>
>Learn Hex!
>>
>b '11 0101 1011';
>0x 3 5 b
>>
>int msg = 0x35b;
Quote:
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.)

Ben Bacarisse
Guest
 
Posts: n/a
#10: Jul 27 '07

re: initializing a number in binary form


tguclu <tugrul.guclu@gmail.comwrites:
Quote:
On Jul 26, 12:54 am, pete <pfil...@mindspring.comwrote:
Quote:
>tguclu wrote:
>>
Quote:
Is it possible to initialize an integer in binary form ? For example
like this?
>>
Quote:
int CrcHesapla()
{
int reg = 0x00;
int msg = b'1101011011';
}
>>
>Learn Hex!
>>
>b '11 0101 1011';
>0x 3 5 b
>>
>int msg = 0x35b;
Quote:
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.
Closed Thread


Similar C / C++ bytes