// floatconvDlg.cpp : implementation file // #include "stdafx.h" #include "floatconv.h" #include "floatconvDlg.h" #include "math.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFloatconvDlg dialog CFloatconvDlg::CFloatconvDlg(CWnd* pParent /*=NULL*/) : CDialog(CFloatconvDlg::IDD, pParent) { //{{AFX_DATA_INIT(CFloatconvDlg) m_Byte1Str = _T(""); m_Byte2Str = _T(""); m_Byte3Str = _T(""); m_Byte4Str = _T(""); m_FloatFormat = -1; m_Exp = 0; m_Mant = _T(""); m_Float = _T(""); m_Note = _T(""); m_Info = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CFloatconvDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CFloatconvDlg) DDX_Control(pDX, IDC_BYTE4, m_Byte4Control); DDX_Text(pDX, IDC_BYTE1, m_Byte1Str); DDV_MaxChars(pDX, m_Byte1Str, 2); DDX_Text(pDX, IDC_BYTE2, m_Byte2Str); DDV_MaxChars(pDX, m_Byte2Str, 2); DDX_Text(pDX, IDC_BYTE3, m_Byte3Str); DDV_MaxChars(pDX, m_Byte3Str, 2); DDX_Text(pDX, IDC_BYTE4, m_Byte4Str); DDV_MaxChars(pDX, m_Byte4Str, 2); DDX_Radio(pDX, IDC_RADIOHT24, m_FloatFormat); DDX_Text(pDX, IDC_EXP, m_Exp); DDX_Text(pDX, IDC_MANT, m_Mant); DDX_Text(pDX, IDC_FLOAT, m_Float); DDX_Text(pDX, IDC_NOTE, m_Note); DDX_Text(pDX, IDC_INFO, m_Info); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CFloatconvDlg, CDialog) //{{AFX_MSG_MAP(CFloatconvDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_B2F, OnB2f) ON_BN_CLICKED(IDC_F2B, OnF2b) ON_EN_MAXTEXT(IDC_BYTE1, OnMaxtextByte1) ON_BN_CLICKED(IDC_RADIOHT24, OnHT24) ON_BN_CLICKED(IDC_RADIOIEEE32, OnIEEE32) ON_BN_CLICKED(IDC_RADIOMCHIP24, OnMCHIP24) ON_BN_CLICKED(IDC_RADIOMCHIP32, OnMCHIP32) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CFloatconvDlg message handlers BOOL CFloatconvDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_FloatFormat = 0; // hitech m_Note = "Note:"; /* m_Info = "Hitech C compiler, ref. Hitech C Compiler manual p81\n"\ "SEEE EEEE EMMM MMMM MMMM MMMM\n"\ "S sign, E exponent, M mantissa\n";*/ if (!m_Info.LoadString(IDS_HITECH24)) m_Info = "Hitech info string not found"; m_Byte1Str = "0"; m_Byte2Str = "0"; m_Byte3Str = "0"; m_Byte4Str = "0"; m_Byte4Control.SetReadOnly(TRUE); m_Float = "0"; UpdateData(FALSE); return TRUE; // return TRUE unless you set the focus to a control } void CFloatconvDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CFloatconvDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CFloatconvDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CFloatconvDlg::OnB2f() { double mantissa=0; int biased_exponent=0; char *endstr; BYTE Byte1, Byte2, Byte3, Byte4; endstr = "\0"; // used by strtoul function UpdateData(TRUE); // load user values Byte1 = (BYTE)strtoul(m_Byte1Str, &endstr, 16); Byte2 = (BYTE)strtoul(m_Byte2Str, &endstr, 16); Byte3 = (BYTE)strtoul(m_Byte3Str, &endstr, 16); Byte4 = (BYTE)strtoul(m_Byte4Str, &endstr, 16); m_Note = "Note:"; // what format are we using? switch (m_FloatFormat) { case 0 : // Hitech 24bit (truncated IEEE 32bit, bias 127, but no Inf or NaN) biased_exponent = ((Byte1 & 0x7F) << 1) + ((Byte2 & 0x80) >> 7); m_Exp = biased_exponent - 127; // removing bias // 15 is number of bits in mantissa, thus the 15 bit right shift mantissa = ((double)(((Byte2 & 0x7F) << 8) + Byte3)) / pow(2, 15) + 1.0; // sign bit set? if (Byte1 & 0x80) mantissa *= -1; if ((Byte1 + Byte2 + Byte3) == 0) { // special case, all zeroes m_Float = "0"; m_Mant.Format("%1.12G", mantissa); m_Note = "Note: Special Case - All bytes are zero"; } else { // output for user m_Mant.Format("%1.12G", mantissa ); m_Float.Format("%1.12G", mantissa * pow(2.0, (double)m_Exp)); } break; case 1 : // IEEE754 32bit biased_exponent = ((Byte1 & 0x7F) << 1) + ((Byte2 & 0x80) >> 7); m_Exp = biased_exponent - 127; // removing bias // 23 is number of bits in mantissa, thus the 23 bit right shift mantissa = ((double)(((Byte2 & 0x7F) << 16) + (Byte3 << 8) + Byte4)) / pow(2, 23) + 1.0; // sign bit set? if (Byte1 & 0x80) mantissa *= -1; if ((Byte1 + Byte2 + Byte3 + Byte4) == 0) { // special case, all zeroes m_Float = "0"; m_Mant.Format("%1.12G", mantissa); m_Note = "Note: Special Case - All bytes are zero"; } else if ((biased_exponent == 0xFF) && ((((Byte2 & 0x7F) << 16) + (Byte3 << 8) + Byte4) == 0x000000)) { // this is also infinity m_Note = "Note: Special Case - Infinity"; } else if ((biased_exponent == 0xFF) && ((((Byte2 & 0x7F) << 16) + (Byte3 << 8) + Byte4) != 0x000000)) { // not a number m_Note = "Note: Special Case - Not A Number (NaN)"; } else { // output for user m_Mant.Format("%1.12G", mantissa ); m_Float.Format("%1.12G", mantissa * pow(2, m_Exp)); } break; case 2 : // Microchip 32Bit biased_exponent = Byte1; m_Exp = biased_exponent - 127; // removing bias // 23 is number of bits in mantissa, thus the 23 bit right shift mantissa = ((double)(((Byte2 & 0x7F) << 16) + (Byte3 << 8) + Byte4)) / pow(2, 23) + 1.0; // sign bit set? if (Byte2 & 0x80) mantissa *= -1; if ((Byte1 + Byte2 + Byte3 + Byte4) == 0) { // special case, all zeroes m_Float = "0"; m_Mant.Format("%1.12G", mantissa); m_Note = "Note: Special Case - All bytes are zero"; } else { // output for user m_Mant.Format("%1.12G", mantissa ); m_Float.Format("%1.12G", mantissa * pow(2, m_Exp)); } break; case 3 : // Microchip 24bit biased_exponent = Byte1; m_Exp = biased_exponent - 127; // removing bias // 15 is number of bits in mantissa, thus the 15 bit right shift mantissa = ((double)(((Byte2 & 0x7F) << 8) + Byte3)) / pow(2, 15) + 1.0; // sign bit set? if (Byte2 & 0x80) mantissa *= -1; if ((Byte1 + Byte2 + Byte3) == 0) { // special case, all zeroes m_Float = "0"; m_Mant.Format("%1.12G", mantissa); m_Note = "Note: Special Case - All bytes are zero"; } else { // output for user m_Mant.Format("%1.12G", mantissa ); m_Float.Format("%1.12G", mantissa * pow(2, m_Exp)); } break; default: break; } // switch (m_FloatFormat) UpdateData(FALSE); } void CFloatconvDlg::OnF2b() { double x, z, UserFloat=0; unsigned long mantissa=0; int e, k; char *endstr, sign; BYTE Byte1, Byte2, Byte3, Byte4, be; endstr = "\0"; // used by strtod function UpdateData(TRUE); // load user values UserFloat = strtod(m_Float, &endstr); // remember sign and keep positive if (UserFloat < 0) { sign = 1; UserFloat *= -1; } else sign = 0; // what format are we using? switch (m_FloatFormat) { case 0 : // Hitech 24bit if (UserFloat == 0.0) { e = -127; Byte1 = 0; Byte2 = 0; Byte3 = 0; Byte4 = 0; mantissa = 1.0; m_Note = "Note: Special Case - Float is zero"; } else { z = log(UserFloat) / log(2.0); e = (int)(z); if ((double)e > z) e --; be = e + 127; x = UserFloat / pow(2.0, (double)e); for (k=0; k>-15; k--) { if (x >= pow(2.0, k)) { mantissa++; // set bit x -= pow(2.0, k); // update remainder } mantissa = mantissa << 1; } // fill byte values if (sign) Byte1 = 0x80; else Byte1 = 0; Byte1 += (be & 0xFE) >> 1; Byte2 = (be & 0x01) << 7; Byte2 += ((int)mantissa & 0x7F00) >> 8; Byte3 = (int)mantissa & 0xFF; Byte4 = 0; m_Mant.Format("%1.12G", ((double)mantissa / pow(2.0, 15))); } break; case 1 : // IEEE754 32Bit if (UserFloat == 0.0) { e = -127; Byte1 = 0; Byte2 = 0; Byte3 = 0; Byte4 = 0; mantissa = 1.0; m_Note = "Note: Special Case - Float is zero"; } else { z = log(UserFloat) / log(2.0); e = (int)(z); if ((double)e > z) e --; be = e + 127; x = UserFloat / pow(2.0, (double)e); for (k=0; k>-23; k--) { if (x >= pow(2.0, k)) { mantissa++; // set bit x -= pow(2.0, k); // update remainder } mantissa = mantissa << 1; } // fill byte values if (sign) Byte1 = 0x80; else Byte1 = 0; Byte1 += (be & 0xFE) >> 1; Byte2 = (be & 0x01) << 7; Byte2 += ((int)mantissa & 0x7F0000) >> 16; Byte3 = ((int)mantissa & 0xFF00) >> 8; Byte4 = (int)mantissa & 0xFF; m_Mant.Format("%1.12G", ((double)mantissa / pow(2.0, 23))); } break; case 2 : // Microchip 32Bit if (UserFloat == 0.0) { e = -127; Byte1 = 0; Byte2 = 0; Byte3 = 0; Byte4 = 0; mantissa = 1.0; m_Note = "Note: Special Case - Float is zero"; } else { z = log(UserFloat) / log(2.0); e = (int)(z); if ((double)e > z) e --; be = e + 127; x = UserFloat / pow(2.0, (double)e); for (k=0; k>-23; k--) { if (x >= pow(2.0, k)) { mantissa++; // set bit x -= pow(2.0, k); // update remainder } mantissa = mantissa << 1; } // fill byte values Byte1 = be; if (sign) Byte2 = 0x80; else Byte2 = 0; Byte2 += ((int)mantissa & 0x7F0000) >> 16; Byte3 = ((int)mantissa & 0xFF00) >> 8; Byte4 = (int)mantissa & 0xFF; m_Mant.Format("%1.12G", ((double)mantissa / pow(2.0, 23))); } break; case 3 : // Microchip 24Bit if (UserFloat == 0.0) { e = -127; Byte1 = 0; Byte2 = 0; Byte3 = 0; Byte4 = 0; mantissa = 1.0; m_Note = "Note: Special Case - Float is zero"; } else { z = log(UserFloat) / log(2.0); e = (int)(z); if ((double)e > z) e --; be = e + 127; x = UserFloat / pow(2.0, (double)e); for (k=0; k>-15; k--) { if (x >= pow(2.0, k)) { mantissa++; // set bit x -= pow(2.0, k); // update remainder } mantissa = mantissa << 1; } // fill byte values Byte1 = be; if (sign) Byte2 = 0x80; else Byte2 = 0; Byte2 += ((int)mantissa & 0x7F00) >> 8; Byte3 = (int)mantissa & 0xFF; Byte4 = 0; m_Mant.Format("%1.12G", ((double)mantissa / pow(2.0, 15))); } break; } m_Byte1Str.Format("%X", Byte1); m_Byte2Str.Format("%X", Byte2); m_Byte3Str.Format("%X", Byte3); m_Byte4Str.Format("%X", Byte4); m_Exp = e; UpdateData(FALSE); } void CFloatconvDlg::OnMaxtextByte1() { // Switch focus to next edit box // CWnd *editboxptr; // editboxptr = GetDlgItem(IDC_FLOAT); LONG WAY // GotoDlgCtrl(editboxptr); NextDlgCtrl(); } void CFloatconvDlg::OnHT24() { UpdateData(TRUE); m_Info.LoadString(IDS_HITECH24); m_Byte4Str = "0"; m_Byte4Control.SetReadOnly(TRUE); UpdateData(FALSE); } void CFloatconvDlg::OnIEEE32() { UpdateData(TRUE); m_Info.LoadString(IDS_IEEE32); m_Byte4Control.SetReadOnly(FALSE); UpdateData(FALSE); } void CFloatconvDlg::OnMCHIP24() { UpdateData(TRUE); m_Info.LoadString(IDS_MCHIP24); m_Byte4Str = "0"; m_Byte4Control.SetReadOnly(TRUE); UpdateData(FALSE); } void CFloatconvDlg::OnMCHIP32() { UpdateData(TRUE); m_Info.LoadString(IDS_MCHIP32); m_Byte4Control.SetReadOnly(FALSE); UpdateData(FALSE); }
file: /Techref/microchip/math/floatconv/floatconvDlg.cpp, 14KB, , updated: 2000/12/16 15:25, local time: 2024/12/27 20:36,
18.191.63.237:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://ecomorder.com/Techref/microchip/math/floatconv/floatconvDlg.cpp"> microchip math floatconv floatconvDlg</A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Welcome to ecomorder.com! |
.