Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Von c++ nach delphi Float Converter (https://www.delphipraxis.net/62746-von-c-nach-delphi-float-converter.html)

enzo 8. Feb 2006 21:51


Von c++ nach delphi Float Converter
 
Liste der Anhänge anzeigen (Anzahl: 1)
Da der Converter den ich beigefuegt habe Ihn C++ geschrieben und Ich kein Spezialist bin.

Da Ich serielle Daten empfange und mit der Funktion Hex in Float Umwandeln muss.

Habe das Programm in C++ probiert und es gibt mir die Richtigen Werte.

Wer Kann mir Helfen.

Danke ihm voraus

Gruss enzo

enzo 8. Feb 2006 22:16

Re: Von c++ nach delphi Float Converter
 
Folgendes interessiert mich das es ihn delphi uebersetzt wird


[c]void CFloatCnvtDlg::OnKillfocusHexEdit()
{
UINT nType;
int iLen, iCnt, iDataCtr;
unsigned char bData;

// Make a union of a float and it's four bytes.
union FloatHex{
unsigned char uData[4]; // Four bytes to hold an IEEE 754 float
float fValue; // The IEEE 754 float
}uConvert;

// Declare a string variable to hold the ASCII HEX form, and temp byte buffer.
CString strHex, strByte;


// Read user entered string from the Hex edit box.
m_HexEdit.GetWindowText(strHex);

// Convert HEX string to uppercase before conversion.
strHex.MakeUpper();

// Check for proper length of HEX string before conversion.
iLen = strHex.GetLength();

// Error checking - Check for invalid hex values.
// Display standard message box if non-Hex char found.
if (iLen == 8) {
for (iCnt=0; iCnt<iLen; ++iCnt){
bData = strHex.GetAt(iCnt);
if ( bData < '0' || (bData > '9' && bData < 'A') || bData > 'F' )
{
CString strCaption;
strCaption = "Bad Info";
nType = MB_ICONERROR;
CString strMsgText = "Please enter valid HEX string only.";
MessageBox( strMsgText, strCaption, nType); // Show msg box

// Setting iLen to -1 stops this loop and disables conversion.
iLen = -1;
}
}
}

// Conversion of ASCII Hex string to hex bytes.
if (iLen == 8) // MUST be 8 chars. (2 chars per byte, 4 bytes per float.)
{
//Loop through the HEX chars, and convert to byte values
//Because we read the float HEX string MSB to LSB, we must store
//the data MSB to LSB. So, iDataCtr was created to help readability.
iDataCtr = (iLen / 2) - 1; //2 text chars per HEX byte. (0-based counter)

for (iCnt=0; iCnt<iLen; ++iCnt){
//Read high nibble char & store
bData = strHex.GetAt(iCnt); //Read high nibble char from string
if (bData > '9') //Convert ASCII A-F to decimal 10-15
bData -= 7;
bData <<= 4; //move to high nib & store
uConvert.uData[iDataCtr] = bData;

// Get low nibble (next char in string)
++iCnt; //inc char ptr
bData = strHex.GetAt(iCnt); //get next char (low nibble)
if (bData > '9') //Convert ASCII A-F to decimal 10-15
bData -= 7;
bData -= '0'; //convert from ASCII to hex
uConvert.uData[iDataCtr] |= bData;//merge hex value

--iDataCtr; //dec data union ptr
}
m_Float = uConvert.fValue; // Update the float edit variable
UpdateData(FALSE); // Update the dialog box (calls DDX).
}
// Error reporting. Skip this if an invalid Hex value was entered. (marked by iLen = -1)
else if (iLen != -1) {
// If we got here, either more than 8 or less than 8 chars were entered.
CString strCaption;
if (iLen < 8) { // Show msg for less than 8 chars
strCaption = "Not Enough Info";
nType = MB_ICONEXCLAMATION;
}
if (iLen > 8) { // Show msg for more than 8 chars
strCaption = "Too Much Info!";
nType = MB_ICONQUESTION;
}
CString strMsgText = "Please enter 4 Hex bytes";
MessageBox( strMsgText, strCaption, nType); // Show msg box
}

}





Ich hoffe Ihr koennt mir Helfen


Gruss enzo


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:08 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz