Conversion between float and hexadecimal in VBA use.

Some times we need a VBA function to perform the conversion between float and hexademical data. After searching the web site , I got a copy of codes to use. Until now, it worked OK, so share with you.  The code was not original made by myself, I just copied it. So, if you (author) read this, you can contact me to decide whether to delete.

'This is VBS code.
'===============================================================================
' User-defined data types
' (Necessary because that's the only way LSET works)
'===============================================================================
Type uLng: l As Long:   End Type
Type uFlt: f As Single: End Type

'===============================================================================
' Functions for Singles (church dances)
'===============================================================================
Function Float2Hex(s As Single) As String
    ' shg 2008-0919
    
    ' Returns the conversion of float s to a hex string
    
    Dim uf As uFlt
    Dim ul As uLng
    
    uf.f = s
    LSet ul = uf
    Float2Hex= Hex(ul.l)
End Function


Function Hex2Float(s As String) As String
	' Returns the conversion of a hex string to float
	Dim uf As uFlt
	Dim ul As uLng

	ul.l = Val("&H" & s)

	LSet uf = ul
	If (ul.l = 0) Then
		Hex2Float = 0#
	Else
		Hex2Float = IIf(uf.f < 1,  "0" & uf.f, uf.f)
	End If
End Function


Sub test()
    Msgbox Hex2Float("40200000")
End sub
Conversion
//Compare with C language.
#ifndef COUNTOF
    #define COUNTOF(__a)  ((sizeof(__a))/(sizeof(*(__a))))
#endif


static void vS_Uchar2ASCIIHex( unsigned char bSrcData, unsigned char *pbString )
{
	unsigned char b4bitData = 0;

    if( NULL != pbString )
    {        
        b4bitData = ( bSrcData >> 4 ) & 0x0f;							//first , high 4-bit
        if( b4bitData > 10 )
        {
            b4bitData = b4bitData + '0';
        }
        else
        {
            b4bitData = b4bitData + 'A' - 10;
        }
        *pbString = b4bitData;
        pbString++;

        b4bitData = bSrcData & 0x0f;									// next , low 4-bit
        if( b4bitData > 10 )
        {
            b4bitData = b4bitData + '0';
        }
        else
        {
            b4bitData = b4bitData + 'A' - 10;
        }
        *pbString = b4bitData;
    }
}

static void vS_Uint2ASCIIHex( unsigned int dwSrc, char *pbDesStr )
{
    unsigned char dwTmp = 0, i = 0;
    if( NULL != pDesStr )
    {
        for( i = 24; i > 0; i -= 8 )
        {
            dwTmp = (unsigned char)(dwSrc >> i);
            vS_Uchar2ASCIIHex( dwTmp, pbDesStr );
            pbDesStr += 2;
        }        
    }
}

void HexFloatConversion( void *pSrcData, float *pDesData, char *pHexStr, bool yRtnType )
{
    Union tagDatasMemory
    {
        float f;
        unsigned int i;
    };
    
    char bHexStr[8] = {'\0'};
    Union tagDatasMemory dm;
    if( NULL == pSrcData )
    {
        return;
    }
    else
    {
        if( yRtnType == TRUE )  //Want to return float
        {
            if( pDesData != NULL )
            {
                dm.i = *((unsigned int*)pSrcData);
                *pDesData = dm.f;
            }
        }
        else                    //Want to return Hexadecimal characters
        {
            if( pHexStr != NULL )
            {
                dm.f = *((float*)pSrcData);
                vS_Uint2ASCIIHex( dm.i, bHexStr );
                memcpy( pHexStr, bHexStr, COUNTOF(bHexStr) );
            }
        }
    }
    
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值