16进制转10进制的算法问题

pdy11 2005-01-23 02:48:26
想要做数码管显示 求16进制转10进制的算法, 16进制转bcd码也可以 请各位高手指点一二 谢谢
...全文
2613 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
三炮 2005-01-28
  • 打赏
  • 举报
回复
也是在csdn里记下来的:
Hexa To Decimal :-
------------------
a)
You can use strtol function and you can specify base.
char * hexstring= "ABCDEF";
char * p;
int number = strtol(hexstring, &p,16);

b)
bool HexToDecimal (char* HexNumber, int& Number)
{
char* pStopString;
Number = strtol (HexNumber, &pStopString, 16);
return (bool)(Number != LONG_MAX);
}

其他的:
PART ONE :- DECIMAL CONVERSIONS

Decimal To Hexa :-
------------------

Use _itoa( ) function and set radix to 16.

char hexstring[10];
int number = 30;
itoa( number, hexstring, 16);

In hexstring is 1e.


Hexa To Decimal :-
------------------
a)
You can use strtol function and you can specify base.
char * hexstring= "ABCDEF";
char * p;
int number = strtol(hexstring, &p,16);

b)
bool HexToDecimal (char* HexNumber, int& Number)
{
char* pStopString;
Number = strtol (HexNumber, &pStopString, 16);
return (bool)(Number != LONG_MAX);
}


Decimal to Time :-
------------------

char *DecToTime(float fTime, char *szTime)
{
int nHrs, nMin, nSec;
fTime *= 3600;
nHrs = (int)fTime / 3600;
nMin = (int)(fTime - nHrs * 3600) / 60;
nSec = (int)(fTime - nHrs * 3600 - nMin * 60);
wsprintf(szTime, "%02d.%02d.%02d Hrs.Min.Sec.", nHrs, nMin, nSec);
return szTime;
}



PART TWO :- STRING CONVERSIONS


String to Hexa :-
-----------------

sscanf(string, %04X, &your_word16);
// where string = your string and 04 = length of your string and X = hex


Hex to CString
---------------

CString Str;
unsigned char Write_Buff[1];
Write_Buff[0] = 0x01;
Str.Format("0x0%x",Write_Buff[0]);


COleVariant to CString
----------------------

CString strTemp;
COleVariant Var;
Var = "FirstName";
strTemp = Var.bstrVal;
AfxMessageBox(strTemp);


CString to Char Pointer
-----------------------
a)
CString MyString = "ABCDEF";
char * szMyString = (char *) (LPCTSTR) MyString;

b)
char *pBuffer = new char[1024];
CString strBuf = "Test";
pBuffer = strBuf.GetBuffer(sizeof(pBuffer));


Char Pointer to CString
-----------------------

char * mystring = "12345";
CString string = mystring;


Double to CString including the fractional part :-
--------------------------------------------------

CString strValue,strInt, strDecimal;
int decimal,sign;
double dValue = 4.125;
strValue = _fcvt(dValue,6,&decimal,&sign); // Now decimal contains 1 because there is only one digit before the .
strInt = strValue.Left(decimal); // strInt contains 4
strDecimal = strValue.Mid(decimal); // strDecimal contains 125

CString strFinalVal;
strFinalVal.Format("%s.%s",strInt,strDecimal); // strFinalVal contains 4.125


Double To CString :-
--------------------
CString strValue;
int decimal,sign;

double dValue = 123456789101112;
strValue = _ecvt(dValue,15,&decimal,&sign); // Converting double to string


CString To Double :-
---------------------

strValue = "121110987654321";
dValue = atof(strValue);


CString to LPCSTR :-
--------------------

CString str1 = _T("My String");
int nLen = str1.GetLength();
LPCSTR lpszBuf = str1.GetBuffer(nLen);
// here do something with lpszBuf...........
str1.ReleaseBuffer();


CString to LPSTR :-
--------------------

CString str = _T("My String");
int nLen = str.GetLength();
LPTSTR lpszBuf = str.GetBuffer(nLen);
// here do something with lpszBuf...........
str.ReleaseBuffer();


CString to WCHAR*
-----------------

CString str = "A string here" ;
LPWSTR lpszW = new WCHAR[255];

LPTSTR lpStr = str.GetBuffer( str.GetLength() );
int nLen = MultiByteToWideChar(CP_ACP, 0,lpStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW, nLen);
AFunctionUsesWCHAR( lpszW );
delete[] lpszW;


LPTSTR to LPWSTR
----------------
int nLen = MultiByteToWideChar(CP_ACP, 0, lptStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lptStr, -1, lpwStr, nLen);


string to BSTR
--------------
string ss = "Girish";
BSTR _bstr_home = A2BSTR(ss.c_str());


CString to BSTR
---------------
CString str = "whatever" ;
BSTR resultsString = str.AllocSysString();


_bstr_t to CString
-------------------

#include
#include
_bstr_t bsText("Hai Bayram");
CString strName;
W2A(bsText, strName.GetBuffer(256), 256);
strName.ReleaseBuffer();
AfxMessageBox(strName);

char szFileName[256];
GetModuleFileName(NULL,szFileName,256);
AfxMessageBox(szFileName);


PART THREE :- CHARACTER ARRAYS

Char array to integer
---------------------

char MyArray[20];
int nValue;

nValue = atoi(MyArray);


Char array to float
-------------------

char MyArray[20];
float fValue;

fValue = atof(MyArray);


Char Pointer to double
----------------------

char *str = " -343.23 ";
double dVal;
dVal = atof( str );


Char Pointer to integer
-----------------------

char *str = " -343.23 ";
int iVal;
iVal = atoi( str );


Char Pointer to long
--------------------

char *str = "99999";
long lVal;
lVal = atol( str );


Char* to BSTR
-------------
char * p = "whatever";
_bstr_t bstr = p;


Float to WORD and Vice Versa
-----------------------------
float fVar;
WORD wVar;
fVar = 247.346;
wVar = (WORD)fVar; //Converting from float to WORD. The value in wVar would be 247
wVar = 247;
fVar = (float)fVar; //Converting from WORD to float. The value in fVar would be 247.00
icesnows 2005-01-28
  • 打赏
  • 举报
回复
这个应该很简单的啊,用C取出各位来,查表显示!
pdy11 2005-01-27
  • 打赏
  • 举报
回复
wihle (hex>100) ;
{hex=hex-100;
bcda=bcda+1;} ;

wihle (hex>10);
{hex=hex-10;
bcdb=bcdb+1;};
wihle (hex>0);
{hex=hex-1;
bcdc=bcdc+1;};
我自己的笨办法 不太会用c语言 就是一个8bit的16进制数用3个字节的bcd码 表示 希望各位有什么巧妙的算法一起发上来
li_hai_quan 2005-01-25
  • 打赏
  • 举报
回复
UBYTE code Segment_Num_Table[10] =
{
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
};是十进制转LED显示码
笨一点的方法如果CPU空间足够大的话直接写出一个16进制转LED显示数组,可以提高CPU的运算速度
pdy11 2005-01-25
  • 打赏
  • 举报
回复
谢谢大家 希望大家都把自己所知道方法写出来 就让这个帖子成为 16转10的答案吧
HOUHOUHOU~~~~~~~~:)
galle 2005-01-25
  • 打赏
  • 举报
回复
//*****************************************************************************
// bcd_to_hex:将BCD格式转换成HEX格式
// Input:形参BCD_Byte
// Output:返回值HEX_Byte
// Notice:无
//*****************************************************************************
unsigned char bcd_to_hex(unsigned char BCD_Byte) small
{
unsigned char data HEX_Byte;
unsigned char data BCD_High_Byte,BCD_Low_Byte;

BCD_High_Byte=BCD_Byte>>4;
BCD_High_Byte=BCD_High_Byte&0x0F;
BCD_Low_Byte=BCD_Byte&0x0F;
HEX_Byte=(BCD_High_Byte)*10+BCD_Low_Byte;
return(HEX_Byte);
}



//*****************************************************************************
// hex_to_bcd:将HEX格式转换成BCD格式
// Input:形参HEX_Byte
// Output:返回值BCD_Byte
// Notice:无
//*****************************************************************************
unsigned char hex_to_bcd(unsigned char HEX_Byte) small
{
unsigned char data BCD_Byte;
unsigned char data BCD_High_Byte=0,BCD_Low_Byte;
while(HEX_Byte>9)
{
HEX_Byte-=10;
BCD_High_Byte++;
}

BCD_High_Byte<<=4;
BCD_Low_Byte=HEX_Byte;
BCD_Byte=BCD_High_Byte+BCD_Low_Byte;
return(BCD_Byte);
}
pdy11 2005-01-24
  • 打赏
  • 举报
回复
to flowercity(小唐"菜") :
2进制怎么算法 能指点一下么 以前书上看到过 现在忘了 又没找到书 谢谢
tlvc 2005-01-24
  • 打赏
  • 举报
回复
void DataconvertBCD( UWORD Val, UBYTE BufLen, UBYTE data *Buf )
{
UBYTE Num;
assert( BufLen <= 6);
Num = 0;
while ( Num < BufLen )
{
*( Buf + BufLen - Num - 1) = Val % 10;
Val = Val / 10;
Num++;
}
}
void DataConvert( UWORD Val, UBYTE BufLen, UBYTE data *Buf )
{
UBYTE Num;
DataconvertBCD( Val,BufLen,Buf);
Num = 0;
while ( Num < ( BufLen - 1) )
{
if ( *( Buf + Num) != 0 )
{
*( Buf + Num) = Segment_Num_Table[*( Buf + Num)];
Num++;
break;
}
Num++;
}
while ( Num < BufLen )
{
*( Buf + Num) = Segment_Num_Table[*( Buf + Num)];
Num++;
}
}
UBYTE code Segment_Num_Table[10] =
{
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
pdy11 2005-01-24
  • 打赏
  • 举报
回复
UBYTE code Segment_Num_Table[10] =
{
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
这几个数是做什么用的
三炮 2005-01-23
  • 打赏
  • 举报
回复
提个建议:

用c语言呢?然后c里面应该有相关函数要不自己写
flowercity 2005-01-23
  • 打赏
  • 举报
回复
方法等同二进制转十进制

27,375

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧