Re: Format real to byte [], NOT string

From: Ignacio Machin \( .NET/ C# MVP \) ("Ignacio)
Date: 10/28/04


Date: Thu, 28 Oct 2004 09:29:55 -0400

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
>
>


Relevant Pages

  • Re: Convert Binary String to Hexadecimal
    ... character representation of an integer value using binary notation. ... The hexadecimal equivalent of the 32-bit binary string ... the characters. ... You don't want your conversion function to open the file and read the ...
    (comp.lang.c)
  • Re: newbie - integer variable with leading zero for os.makedirs
    ... >string from ascii to integer, but loses the first zero padding. ... The same binary number (judging by the decimal representation (488) that's being printed). ... as '750' so the conversion from string to binary integer succeeds and gives the value 750. ...
    (comp.lang.python)
  • Re: How to access/manipulate C structures from multiple Tcl extensions
    ... I'll just return TclError whenever a conversion is ... when converting to a string ... internal structure or something, rather than an actual representation ...
    (comp.lang.tcl)
  • Re: Unicode question
    ... Obviously not for the representation in memory;-) ... They fit your approach to string handling. ... character encoding, or in the average or precise display width of the ... Since UTF-16 chars can be 2 or 4 bytes, one has to be always alert ...
    (borland.public.delphi.non-technical)
  • Re: Strings and bindary data
    ... String to another file, are those files guaranteed to be identical? ... No. Strings are designed to hold textual data, and that /always/ is subject to ... beyond what is in its representation. ... representation -- that's to say a mapping from abstract characters (or ...
    (comp.lang.java.programmer)

Loading