Greetings,
I've been up and down google on this one and clearly I don't know what
search terms to use because I can't locate how to do this for binary values
(or hex)...
in C#, we can specify a floating value by saying 0.0f and a hex number by
saying x = 0xFF10 but how do we specify a binary constant? I thought it
might be something like x = %1010101; but I honestly can't figure out what
it might be or how to locate it in the help file since can't even locate the
ones I do know about (float, decimal, hex, etc).
Anyone know?
Thanks,
Shawn 6 17081
Hi Shawn,
you cannot specify a binary value in C#, however hexadecimal values are
easy to convert to/from binary values so you can use that instead.
The C# language specification is a good resource for looking at language
syntax specific items: http://msdn2.microsoft.com/en-us/vcsharp/aa336809.aspx
HTH
Mark
-- http://www.markdawson.org
"Shawn B." wrote:
Greetings,
I've been up and down google on this one and clearly I don't know what
search terms to use because I can't locate how to do this for binary values
(or hex)...
in C#, we can specify a floating value by saying 0.0f and a hex number by
saying x = 0xFF10 but how do we specify a binary constant? I thought it
might be something like x = %1010101; but I honestly can't figure out what
it might be or how to locate it in the help file since can't even locate the
ones I do know about (float, decimal, hex, etc).
Anyone know?
Thanks,
Shawn
You can't do it as a *literal*, but it can be done indirectly...
Example:
static readonly int MY_CONST = Convert.ToInt32("10010101", 2);
When the class initialiser fires MY_CONST will evaluate the string as
an integer, but it will only ever run this once, so you get similar
performance to a literal [except that it won't be pre-combined with
other "constant" values at the compiler].
Marc
Hi Marc,
Interesting, but it should be noted that hex is a more standardized
approach - I don't see "binary" strings too often ;)
--
Dave Sexton
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
You can't do it as a *literal*, but it can be done indirectly...
Example:
static readonly int MY_CONST = Convert.ToInt32("10010101", 2);
When the class initialiser fires MY_CONST will evaluate the string as
an integer, but it will only ever run this once, so you get similar
performance to a literal [except that it won't be pre-combined with
other "constant" values at the compiler].
Marc
Indeed, and I have *never* used this string approach myself... included in
the context that in some circumstance it *might* be useful to see the bits
directly. Actually I tend to just use integer literals myself. I'm so used
to the 2^n sequence that I can usually spot the bits, and if not "calc" is
just a keypress away (on an extended keyboard, at least ;-p)
Marc
Hi Marc,
Good point. In my experience, when I need to see the bits it's probably
because I'm using flags, so an Enum with FlagsAttribute could be useful (or
possibly BitArray or BitVector32 structures). I can't remember ever needing
to see the bits in a literal, however.
--
Dave Sexton
"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Indeed, and I have *never* used this string approach myself... included in
the context that in some circumstance it *might* be useful to see the bits
directly. Actually I tend to just use integer literals myself. I'm so used
to the 2^n sequence that I can usually spot the bits, and if not "calc" is
just a keypress away (on an extended keyboard, at least ;-p)
Marc
Good point. In my experience, when I need to see the bits it's probably
because I'm using flags, so an Enum with FlagsAttribute could be useful
(or possibly BitArray or BitVector32 structures). I can't remember ever
needing to see the bits in a literal, however.
I'm using my values in an enumeration. I've decided to convert the binary
into hex and use hex literals. I wouldn't normally be attempting such a
thing, but I'm writing a CPU emulator at the opcode chart is listed binary
numbers instead of hex... simply becuase the instuction, bitflags,
conditinals, source and destination are all packed into a single 32-bit
value so they use binary in the opcode charts.
If anyone cares, its the 32-bit 8-Core CPU from parallax.
For now, I'm converted the binary into hex and is sufficient for my rather
simple needs.
Thanks,
Shawn This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul |
last post by:
Hi,
(First apologies if this is not the most relevant place to post this
but I wasn't sure of where was and I am writing my app in VB.)
I'm attempting to parse a binary file for which I have...
|
by: Rune Froysa |
last post by:
Trying something like::
import xmlrpclib
svr = xmlrpclib.Server("http://127.0.0.1:8000")
svr.test("\x1btest")
Failes on the server with::
xml.parsers.expat.ExpatError: not well-formed (invalid...
|
by: knapak |
last post by:
Hello
I'm a self instructed amateur attempting to read a huge file from disk... so
bear with me please... I just learned that reading a file in binary is
faster than text. So I wrote the...
|
by: Adam J. Schaff |
last post by:
I am writing a quick program to edit a binary file that contains file paths
(amongst other things). If I look at the files in notepad, they look like:
...
|
by: |
last post by:
Hi all, is there a better way to stream binary data stored in a table in sql
2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We
noticed that in certain heavy load scenarios,...
|
by: elliotng.ee |
last post by:
I have a text file that contains a header 32-bit binary. For example,
the text file could be:
%%This is the input text
%%test.txt
Date: Tue Dec 26 14:03:35 2006...
|
by: Leo jay |
last post by:
i'd like to implement a class template to convert binary numbers to
decimal at compile time.
and my test cases are:
BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
|
by: waterdriven |
last post by:
Hello;
I am a newbie.
A homework assignment was assigned and I am having trouble getting started.
The assignment reads:
Write a program to print out the binary value of a 16 bit number.
|
by: Bryan.Fodness |
last post by:
Hello,
I am having trouble writing the code to read a binary string. I would
like to extract the values for use in a calculation.
Any help would be great.
Here is my function that takes in...
|
by: Frinavale |
last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number
This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |