473,788 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get data of internal data representation of primitive type

Is there a way to collectively get the internal data for primitive types? So
for Int32, SByte, etc, I want to be able to get the byte stream used for the
data.

The reason is that I want a byte stream of the values but I don't want to
convert each one by hand(I started but its getting messy).

For, since double is a 64-bit type I'd like to get the actual bits in memory
so I can write them to a stream instead of converting the double to a byte
stream myself.

I'm thinking I'll probably have to use unsafe code and pointers but just
wanted to check and see if there was an easier way.

Thanks,
Jon
Apr 1 '08 #1
5 1699
BitConverter.Ge tBytes(...) and BitConverter.To[...]() to get back the
primative.

but watch for BitConverter.Is LittleEndian - i.e. you might want to
know the endian-ness of your stream, otherwise it will go crazy if you
write on a Pentium etc (little-endian) and read on an Itanium (big-
endian). Jon Skeet has an EndianBitConver ter than might help:
http://www.pobox.com/~skeet/csharp/miscutil/

Marc
Apr 2 '08 #2

"Marc Gravell" <ma**********@g mail.comwrote in message
news:af******** *************** ***********@2g2 000hsn.googlegr oups.com...
BitConverter.Ge tBytes(...) and BitConverter.To[...]() to get back the
primative.

but watch for BitConverter.Is LittleEndian - i.e. you might want to
know the endian-ness of your stream, otherwise it will go crazy if you
write on a Pentium etc (little-endian) and read on an Itanium (big-
endian). Jon Skeet has an EndianBitConver ter than might help:
http://www.pobox.com/~skeet/csharp/miscutil/

Unfortunately I do not know the type that I am converting except that it is
a value type.
Apr 2 '08 #3
Ah; you stated <q>for primitive types</q>. As per a separate chain,
there is a big difference between a primative* type and a value-type.
You cannot serialize most value-types in the way you are trying. For
example: what does this contain?

public struct Pair
{
private readonly int value;
private readonly string name;
public int Value{ get { return value; } }
public string Name { get { return name; } }
public Pair(string name, int value)
{
this.name = name;
this.value = value;
}
}

The "value" is fine (just about), but the "name" is a reference. The
binary value is the /address/ of the string; serializing a memory
address is always** a bug. As an aside, the size of the address will
itself change between 32-bit and 64-bit.
Likewise, this could be *any* reference-type, not just string; for
example:

KeyValuePair<St ream, Form>

is a perfectly well defined struct...

Now - you could argue that you only want to serialize structs that are
only composed of structs, but the reality is that string is so
important that this is not going to be all that useful...

*: by this I'm meaning the predefined types as per section 8.2.1 of
ECMA 334
**: unless you're writing a debugger!
Apr 2 '08 #4
I saida big difference between a primative* type and a value-type

Before things get confusing; can I clarify: I mean that the predefined
types are a small subset of value-types; i.e. an int (or Int32) *is* a
value-type; I don't mean that they are mutually exclusive terms.

Marc
Apr 2 '08 #5
Not sure what your talking about. My write code just recurses over all
types. If its a value type then I write it out to file, else its either a
field or property.
??? value-type vs reference-type relates to types; field vs property
relates to members... it can be a value-type *and* a field or property
member...
(and it should work for arbitrary ones I imagine)
I disagree; "arbitrary" has to include reference-types, which you
aren't considering.
but it probably will just end up making a mess.
I'm thinking you already got there... sorry, but I *really* think
you're deeply over your head here. Custom serialization *of arbitrary
types* is not trivial. I'm not even going to /begin/ to unpick that
code, especially with the unsafe hacks. Serializing your own known
type (from the inside) is fair enough; implement ISerializable and the
custom ctor - job done. But before trying to out-do the serializer, I
would want to *know* that the regular serialization engine isn't good
enough (i.e. have a definition of "good enough"). I'm not convinced
that this is going to be worth the development / support cost... I'd
look at simply zipping what you have, picking either xml or binary
serialization as most appropriate, and trying a few different
compression algorithms (gzip, deflate, bzip2, etc), perhaps from a few
providers (ms, #ziplib, etc). That would take, at worst, 20 minutes.
With zero ongoing cost.

Marc
Apr 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4539
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
5
1397
by: Batista, Facundo | last post by:
I'm proud to announce that the PEP for Decimal Data Type is now published under the python.org structure: http://www.python.org/peps/pep-0327.html This wouldn't has been possible without the help from Alex Martelli, Aahz, Tim Peters, David Goodger and c.l.p itself. After the pre-PEP roundups the features are almost established. There is not agreement yet on how to create a Decimal from a float, in both explicit and
5
2403
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and relevant announcements. If you're interested, drop me a note. But if for some reason you think that this discussion is fine here at the c.l.py, please let me know. ** LONG POST AHEAD **
4
3863
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own malloc to do dynamic allocation from some static pool isn't really an option, for various reasons, not...
17
2273
by: Mantorok Redgormor | last post by:
are all integers represented internally as just bit vectors? -- nethlek
5
3498
by: Sathyaish | last post by:
When you say char in C, it internally means "an unsigned small integer with 1-byte memory", right? More importantly, the internal representation of char does not mean "int" as in "machine-dependant/register-size dependant integer, which is normally four-byte on 32-bit processors", right?
26
12020
by: Kip | last post by:
Greetings everyone, Is there anyone here who could point me to a page or pdf that has a list of the sizes of all of the C primitive data types on various implementations such as SPARC, x86, PowerPC, etc? Thank you. -- Kip Warner
14
7445
by: Matt | last post by:
I want to know if "int" is a primitive type, or an object? For example, the following two approaches yield the same result. > int t1 = int.Parse(TextBox2.Text); //method 1 > int t2 = System.Int32.Parse(TextBox2.Text); //method 2 And people said "int" is a C# alias for "System.Int32". If this is the case, can we say "int" is an object??
11
3825
by: itdevries | last post by:
Hi, I'm trying to convert some char data I read from a binary file (using ifstream) to a float type. I've managed to convert the int types but now I need to do the float types as well but it doesn't seem to work. The code below is what I'm trying to use. Anyone see any obvious errors? or have any hints/pointers? regards, Igor float floatRead = 0;
0
9656
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10366
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10175
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.