473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1680
BitConverter.GetBytes(...) and BitConverter.To[...]() to get back the
primative.

but watch for BitConverter.IsLittleEndian - 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 EndianBitConverter than might help:
http://www.pobox.com/~skeet/csharp/miscutil/

Marc
Apr 2 '08 #2

"Marc Gravell" <ma**********@gmail.comwrote in message
news:af**********************************@2g2000hs n.googlegroups.com...
BitConverter.GetBytes(...) and BitConverter.To[...]() to get back the
primative.

but watch for BitConverter.IsLittleEndian - 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 EndianBitConverter 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<Stream, 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
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
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...
5
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...
4
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...
17
by: Mantorok Redgormor | last post by:
are all integers represented internally as just bit vectors? -- nethlek
5
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...
26
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,...
14
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 =...
11
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.