473,405 Members | 2,141 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,405 software developers and data experts.

Format real to byte [], NOT string

Hi.

Is there a way to utilize the great primitive data type
formatting routines available in .NET without working with
strings?

I want a byte [] directly rather than a string.
I think it is unfortunate that i have to walk via System.String to do that.

For instance, from an integer of value 123, i want:
new byte [3] { ASCII.Digit1, ASCII.Digit2, ASCII.Digit3 }

I have managed to write my own formatting routines for
integer types ushort, short, uint, int, ulong and long
that create a byte array directly without walking via string.

But the floating point formatting routines is kind of complicated,
i rather not writing them if someone else has already done it and are
willing to share
that or (but i have never seen anything like it) if the .NET framework
provides something
for this. I am looking to be as efficient as possible
(that is why i am interested in this in the first place).

By now, i have to do a Math.Round, then format the number into string(also
passing a NumberFormatInfo),
then create a byte array from that. It is just too much overhead.

I always want the real numbers formatted with a maximum precision of 5
decimals,
using '.' for number group separator.
For instance, from a float of value 3.14F, i want:
new byte [4] { ASCII.Digit3, ASCII.Dot, ASCII.Digit1, ASCII.Digit4 };
If there is nothing like it, do you know where to find information
about the internal bit-level RAM representation of System.Single/Double?
How can i solve this?


--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Nov 16 '05 #1
4 2388
Dennis Myrén <de****@oslokb.no> wrote:
Is there a way to utilize the great primitive data type
formatting routines available in .NET without working with
strings?

I want a byte [] directly rather than a string.
I think it is unfortunate that i have to walk via System.String to do that.
Why? Do you have any evidence that it's actually hurting your
performance significantly? I'd try things the simple way and then see
whether the performance is good enough before rushing to write custom
formatting code.
For instance, from an integer of value 123, i want:
new byte [3] { ASCII.Digit1, ASCII.Digit2, ASCII.Digit3 }
I don't know of any way of doing that, no.

<snip>
If there is nothing like it, do you know where to find information
about the internal bit-level RAM representation of System.Single/Double?


They're IEEE 754 numbers, so look for specs for that - there are plenty
around.

You could look at my DoubleConverter code referenced on
http://www.pobox.com/~skeet/csharp/floatingpoint.html for some ideas,
too, but that's not really designed for efficiency particularly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi,

For instance, from an integer of value 123, i want:
new byte [3] { ASCII.Digit1, ASCII.Digit2, ASCII.Digit3 }
I think you have an understanding problem about the types, I will try to
explain the differences but I think it's not an easy going.

An integer is just a set of bits , you can interprete it in different ways,
if you use decimal you may see it as 123 , if you use hex you get 7B, the
thing is that THE SAME sequence of bits is interpreted in different ways
(maybe the "interpreted" word is not the best , printed is more accurate ).
Also remember that 123 as an integer is a whole thing, is not a thing
composed of three subcomponents ( 1 , 2, 3 ) that you can access it
independently.

This is where the conversion takes part, you want to CHANGE the way an
integer is represented, hence you have to make some operations, you can have
two options:
1- Convert to a string, this work cause the string representation of 123 is
composed of 3 "subcomponents" that you can access independently : '1' , '2',
'3'
2- Make the conversion using numeric method:
digit1 = 123/100

...
A similar thing with the double
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


I have managed to write my own formatting routines for
integer types ushort, short, uint, int, ulong and long
that create a byte array directly without walking via string.

But the floating point formatting routines is kind of complicated,
i rather not writing them if someone else has already done it and are
willing to share
that or (but i have never seen anything like it) if the .NET framework
provides something
for this. I am looking to be as efficient as possible
(that is why i am interested in this in the first place).

By now, i have to do a Math.Round, then format the number into string(also
passing a NumberFormatInfo),
then create a byte array from that. It is just too much overhead.

I always want the real numbers formatted with a maximum precision of 5
decimals,
using '.' for number group separator.
For instance, from a float of value 3.14F, i want:
new byte [4] { ASCII.Digit3, ASCII.Dot, ASCII.Digit1, ASCII.Digit4 };
If there is nothing like it, do you know where to find information
about the internal bit-level RAM representation of System.Single/Double?
How can i solve this?


--
Regards,
Dennis JD Myrén
Oslo Kodebureau

Nov 16 '05 #3
Hi, and thanks for reply.

Yes, i know integers is not stored the way i want them formatted in memory.
But i have no problem with formatting integers(all types) to byte arrays,
it was just an example to let you know what i wanted,
what i am really interested in is a way of formatting real numbers
without creating one or more System.String instance first.

If you are interested, i will include an example of the function i came up
with for formatting a 32-bit integer and writing directly to a stream at the
end of this message.

However, System.Single/Double is not that easy at all, at least
not until i have learnt how it is internally stored.
I have found some Microsoft code that formats floating point numbers
into byte array, but have not got it working properly.
I have not found much more than that.

Here is an example of writing Int32 formatted directly to stream, no string
is created.
<Int32ToByteArrayExample>
public override void Write ( int value )
{
bool negative = 0x0 > value;
byte i = 0xB;
byte [] buf = new byte [i];
if (negative)
if (-10 >= value)
{
buf [-- i] = ByteSequences.base10 [-(value % 0xA)];
value /= -10;
}
else
value = -value;
while (0xA <= value)
{
buf [-- i] = ByteSequences.base10 [value % 0xA];
value /= 0xA;
}
buf [-- i] = ByteSequences.base10 [value];
if (negative)
buf [-- i] = ASCII.Minus;
base.Write(buf, i, 0xB - i);
}
</Int32ToByteArrayExample>

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:e2**************@TK2MSFTNGP14.phx.gbl...
Hi,

For instance, from an integer of value 123, i want:
new byte [3] { ASCII.Digit1, ASCII.Digit2, ASCII.Digit3 }


I think you have an understanding problem about the types, I will try to
explain the differences but I think it's not an easy going.

An integer is just a set of bits , you can interprete it in different
ways,
if you use decimal you may see it as 123 , if you use hex you get 7B, the
thing is that THE SAME sequence of bits is interpreted in different ways
(maybe the "interpreted" word is not the best , printed is more
accurate ).
Also remember that 123 as an integer is a whole thing, is not a thing
composed of three subcomponents ( 1 , 2, 3 ) that you can access it
independently.

This is where the conversion takes part, you want to CHANGE the way an
integer is represented, hence you have to make some operations, you can
have
two options:
1- Convert to a string, this work cause the string representation of 123
is
composed of 3 "subcomponents" that you can access independently : '1' ,
'2',
'3'
2- Make the conversion using numeric method:
digit1 = 123/100

...
A similar thing with the double
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


I have managed to write my own formatting routines for
integer types ushort, short, uint, int, ulong and long
that create a byte array directly without walking via string.

But the floating point formatting routines is kind of complicated,
i rather not writing them if someone else has already done it and are
willing to share
that or (but i have never seen anything like it) if the .NET framework
provides something
for this. I am looking to be as efficient as possible
(that is why i am interested in this in the first place).

By now, i have to do a Math.Round, then format the number into
string(also
passing a NumberFormatInfo),
then create a byte array from that. It is just too much overhead.

I always want the real numbers formatted with a maximum precision of 5
decimals,
using '.' for number group separator.
For instance, from a float of value 3.14F, i want:
new byte [4] { ASCII.Digit3, ASCII.Dot, ASCII.Digit1, ASCII.Digit4 };
If there is nothing like it, do you know where to find information
about the internal bit-level RAM representation of System.Single/Double?
How can i solve this?


--
Regards,
Dennis JD Myrén
Oslo Kodebureau


Nov 16 '05 #4
Thanks Jon.

I have looked at your DoubleConverter, which is really cool, i just have to
break it down to pieces to understand all the code.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dennis Myrén <de****@oslokb.no> wrote:
Is there a way to utilize the great primitive data type
formatting routines available in .NET without working with
strings?

I want a byte [] directly rather than a string.
I think it is unfortunate that i have to walk via System.String to do
that.
Why? Do you have any evidence that it's actually hurting your
performance significantly? I'd try things the simple way and then see
whether the performance is good enough before rushing to write custom
formatting code.
For instance, from an integer of value 123, i want:
new byte [3] { ASCII.Digit1, ASCII.Digit2, ASCII.Digit3 }
I don't know of any way of doing that, no.

<snip>
If there is nothing like it, do you know where to find information
about the internal bit-level RAM representation of System.Single/Double?


They're IEEE 754 numbers, so look for specs for that - there are plenty
around.

You could look at my DoubleConverter code referenced on
http://www.pobox.com/~skeet/csharp/floatingpoint.html for some ideas,
too, but that's not really designed for efficiency particularly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

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

Similar topics

0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
12
by: neutrino | last post by:
Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string...
6
by: J | last post by:
Would anyone know if there a type tag to format a double? I have f for floating point, but cannot find one for double.
27
by: geskerrett | last post by:
I am hoping someone can help me solve a bit of a puzzle. We are working on a data file reader and extraction tool for an old MS-DOS accounting system dating back to the mid 80's. In the data...
6
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
1
by: John Marble | last post by:
I looked around for a way to change the format and the decimal setting of a REAL type variable, but I can't seem to be able to find the synthax anywhere. Here is an exemple of what I am doing: ...
4
by: Serge Klokov | last post by:
Hello! I have a Oracle table with a BLOB field. Each field is actually a simple text file of several lines. How to get this BLOB fields in readable format? I tryied something like...
10
by: Dixie | last post by:
I am appending some new fields to a table in vba and when I append a number field with is a byte, it does not inherit any format. I want it to be the General Number format, but it is blank. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
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...

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.