2010-12-28 4 views

Antwort

2

Ein naheliegender Ansatz wäre die textuelle Darstellung der Zahl als Austauschformat.

+0

@mekrizzy da beide Systeme Schwimmer vom und zum konvertieren Text Sie können Text als Ihre gemeinsame Sprache verwenden. –

3
// http://en.wikipedia.org/wiki/IBM_Floating_Point_Architecture 
// float2ibm(-118.625F) == 0xC276A000 
// 1 100 0010 0111 0110 1010 0000 0000 0000 
// IBM/370 single precision, 4 bytes 
// xxxx.xxxx xxxx.xxxx xxxx.xxxx xxxx.xxxx 
// s|-exp--| |--------fraction-----------| 
// (7)   (24) 
// value = (-1)**s * 16**(e - 64) * .f range = 5E-79 ... 7E+75 
static int float2ibm(float from) 
{ 
    byte[] bytes = BitConverter.GetBytes(from); 
    int fconv = (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8)| bytes[0]; 

    if (fconv == 0) return 0; 
    int fmant = (0x007fffff & fconv) | 0x00800000; 
    int t = (int)((0x7f800000 & fconv) >> 23) - 126; 
    while (0 != (t & 0x3)) { ++t; fmant >>= 1; } 
    fconv = (int)(0x80000000 & fconv) | (((t >> 2) + 64) << 24) | fmant; 
    return fconv; // big endian order 
} 

änderte ich ein Stück Code static void float_to_ibm genannt (int von [], int [], int n, int Endian) den obigen Code korrekt in PC von ausgeführt werden wenig Endian-Float-Nummer. Rückgabewert ist Big-Endian-ibm-Gleitkommazahl, aber in Typ int gespeichert.

+0

Das hat mir wirklich das Leben gerettet, ich wünschte, ich könnte euch 50 Rufpunkte an euch beide beschleunigen und Noam M, ich finde einfach nicht wie. Eine Notiz: In meinem Fall musste ich die Bytes des zurückgegebenen Int in umgekehrter Reihenfolge lesen, um die druckbare Ausgabe in eine Windows-1252-kodierte Datei (Little Endian) zu bekommen. Es war für SAS XPORT. –

0

Ich musste vor kurzem einen Float in einen anderen konvertieren. Es sieht so aus, als ob das XDR-Format ein ungerades Format für seine Floats verwendet. Also, bei der Konvertierung von XDR zu Standard-Floats, hat dieser Code es getan.

#include <rpc/rpc.h> 

// Read in XDR float array, copy to standard float array 
// out array needs to be allocated before the function call 

bool convertFromXdrFloatArray(float *in, float *out, long size) 
{ 
    XDR xdrs; 
    xdrmem_create(&xdrs,(char *)in, size*sizeof(float), XDR_DECODE); 

    for(int i = 0; i < size; i++) 
    { 
     if(!xdr_float(&xdrs, out++)) { 
      fprintf(stderr,"%s:%d:ERROR:xdr_float\n",__FILE__,__LINE__); 
      exit(1); 
     } 
    } 
    xdr_destroy(&xdrs); 

    return true; 
} 
0

Mit ersten Antwort habe ich die folgende, die in einigen Fällen nützlich sein kann:

///

/// Converts an IEEE floating number to its string representation (4 or 8 ascii codes). 
    /// Useful for SAS XPORT files format. 
    /// </summary> 
    /// <param name="from_">IEEE number</param> 
    /// <param name="padTo8_">When true, output is 8 chars rather than 4</param> 
    /// <returns>Printable string according to hardware's endianness</returns> 
    public static string Float2IbmAsAsciiCodes(float from_, bool padTo8_ = true) 
    { 
     StringBuilder sb = new StringBuilder(); 
     string s; 
     byte[] bytes = BitConverter.GetBytes(Float2Ibm(from_)); // big endian order 

     if (BitConverter.IsLittleEndian) 
     { 
      // Revert bytes order 
      for (int i = 3; i > -1; i--) 
       sb.Append(Convert.ToChar(bytes[i])); 
      s = sb.ToString(); 
      if (padTo8_) 
       s = s.PadRight(8, '\0'); 
      return s; 
     } 
     else 
     { 
      for (int i = 0; i < 8; i++) 
       sb.Append(Convert.ToChar(bytes[i])); 
      s = sb.ToString(); 
      if (padTo8_) 
       s = s.PadRight(8, '\0'); 
      return s; 
     } 
    } 
Verwandte Themen