有更简单的方法吗? [英] Is there a simpler way?
问题描述
我正在尝试学习VC.NET并转换一些Borland C ++应用程序。
语法差异让我感到害怕...
有没有一种简单的方法可以将用户输入的十六进制字符串转换为
二进制字符串并返回?这是我想要转换的实际代码。
HexToBin(KeyEdit-> Text.c_str(),BinKey,sizeof(BinKey));
>
HexToBin是一个通用函数,用于转换类似1b34c38d的字符串。
它是二进制等价物。还有一个BinToHex可以反转
过程。
我知道我可以编写一个函数来完成它,但是我抛出了所有这些例程
10年前,当我开始使用Borland时,
远离。
我希望有人可以提供帮助,因为我真的不想要这样做
回去编写所有这些小转换例程。
请参阅Int32的方法。 Int32-> Parse可以将十六进制字符串转换为
二进制,而Int32-> ToString可以从二进制转换为十六进制字符串。玩得开心!
" Fred Hebert"写道:
我正在尝试学习VC.NET并转换一些Borland C ++应用程序。
语法差异让我感到害怕...
是否有一种简单的方法可以将用户输入的十六进制字符串转换为
二进制字符串并返回?这是我想要转换的实际代码。
HexToBin(KeyEdit-> Text.c_str(),BinKey,sizeof(BinKey));
HexToBin是一个转换字符串的泛型函数,如1b34c38d这是二进制等价的。还有一个BinToHex可以改变
过程。
我知道我可以编写一个函数来完成它,但是10年前当我开始时,我抛弃了所有这些例程。使用Borland。
我希望有人可以提供帮助,因为我真的不想再回去编写所有这些小转换程序。
sscanf可能适合您的目的。
int nHex;
if(1 == sscanf(KeyEdit-> Text.c_str(),"%x",& nHex)){
//转换
}
Fred Hebert写道:我正在尝试学习VC.NET并转换一些Borland C ++应用程序。
语法差异让我感到害怕...
是否有一种简单的方法可以将用户输入的十六进制字符串转换为
二进制字符串并返回?这是我想要转换的实际代码。
HexToBin(KeyEdit-> Text.c_str(),BinKey,sizeof(BinKey));
HexToBin是一个转换字符串的泛型函数,如1b34c38d这是二进制等价的。还有一个BinToHex可以改变
过程。
我知道我可以编写一个函数来完成它,但是10年前当我开始时,我抛弃了所有这些例程。使用Borland。
我希望有人可以提供帮助,因为我真的不想再回去编写所有这些小转换程序。
Hallo Fred!
我正在尝试学习VC.NET并转换一些Borland C ++应用程序。
语法差异让我感到害怕......
为什么?它的C ++
有没有一种简单的方法可以将用户输入的十六进制字符串转换为
二进制字符串并返回?这是我想要转换的实际代码。
HexToBin(KeyEdit-> Text.c_str(),BinKey,sizeof(BinKey));
HexToBin是一个转换字符串的泛型函数,如1b34c38d这是二进制等价的。还有一个BinToHex可以改变
过程。
我知道我可以编写一个函数来完成它,但是10年前当我开始时,我抛弃了所有这些例程。使用Borland。
即使你应该能够编程和学习! SCNR ......
我希望有人可以提供帮助,因为我真的不想再回去编写所有这些小转换例程。
静态内联TCHAR ByteToHex(int c)
{
返回static_cast< TCHAR>((c< 10)?( c)+ _ T(''0''):( c)-10 + _T(''A''));
}
void BinToString(PCVOID pData ,PTSTR pszOut,int iLen)
{
int iPos = 0;
const BYTE * pszStr =(const BYTE *)pData;
while(iLen--)
{
BYTE byte = * pszStr ++;
BYTE uByte =(byte& ; 0xF0)>> 4,
lByte = byte& 0xF;
pszOut [iPos ++] = ByteToHex(uByte);
pszOut [iPos ++] = ByteToHex(lByte);
}
pszOut [iPos] =''\ 0'';
}
静态内联int HexToByte(TCHAR c)
{
return(c> = _ T(''0'')&& c< = _ T(''9''))? (c)-_ T(''0''):
(c> = _ T(''A'')&& c< = _ T(''F''))? (c)-_ T(''A'')+ 10:
(c> = _ T(''a'')&& c< = _ T(''f'') )? (c)-_ T(''a'')+ 10:0;
}
静态内联bool IsHexDigit(TCHAR c)
{
返回c> = _ T(''0'')&& c< = _ T(''9'')||
((c> = _ T(''a'')&& c< = _ T(''f'')) ||
(c> = _ T(''A'')&& c< = _ T(''F'')));
}
int StringToBin(PCTSTR pIn,PVOID pOut,int iLen)
{
if(iLen == - 1)
iLen = _tcslen(pIn)/ 2;
memset(pOut,0,iLen);
if(!pIn)
返回0;
TCHAR uByte,lByte;
int iPos = 0;
PBYTE pszStr = static_cast< PBYTE> ;(pOut);
while(iPos< iLen&& IsHexDigit(uByte = pIn [0])&& IsHexDigit(lByte = pIn [1]))
{
* pszStr ++ = static_cast< BYTE>(HexToByte(uByte)<< 4 | HexToByte(lByte));
++ iPos;
pIn + = 2;
}
返回* pIn? 0:iPos;
}
HTH
-
Martin Richter [MVP] WWJD
在C中我们必须编写自己的错误代码。在C ++中我们可以继承它们。
FAQ: http:// www .mpdvc.de
样品: http:// www。 codeguru.com http://www.codeproject.com
I am trying to learn VC.NET and convert some Borland C++ applications. The
syntax differences are killing me...
Is there an easy way to convert a hex string, entered by the user, to a
binary string and back? This is the actual code I am trying to convert.
HexToBin(KeyEdit->Text.c_str(), BinKey, sizeof(BinKey));
HexToBin is a generic function that converts a string like "1b34c38d" to
it''s binary equivalent. There is also a BinToHex which reverses the
process.
I know I could write a function to do it, but I threw all those routines
away 10 years ago when I started using Borland.
I am hoping that someone can help, because I really don''t want to have to
go back to writing all of those little conversion routines.
Please see the methods of Int32. Int32->Parse can convert hex string to
binary, and Int32->ToString can convert from binary to hex string. Have fun!
"Fred Hebert" wrote:
I am trying to learn VC.NET and convert some Borland C++ applications. The
syntax differences are killing me...
Is there an easy way to convert a hex string, entered by the user, to a
binary string and back? This is the actual code I am trying to convert.
HexToBin(KeyEdit->Text.c_str(), BinKey, sizeof(BinKey));
HexToBin is a generic function that converts a string like "1b34c38d" to
it''s binary equivalent. There is also a BinToHex which reverses the
process.
I know I could write a function to do it, but I threw all those routines
away 10 years ago when I started using Borland.
I am hoping that someone can help, because I really don''t want to have to
go back to writing all of those little conversion routines.
sscanf might suit your purposes.
int nHex;
if (1 == sscanf(KeyEdit->Text.c_str(), "%x", &nHex)) {
// converted
}
Fred Hebert wrote:I am trying to learn VC.NET and convert some Borland C++ applications. The
syntax differences are killing me...
Is there an easy way to convert a hex string, entered by the user, to a
binary string and back? This is the actual code I am trying to convert.
HexToBin(KeyEdit->Text.c_str(), BinKey, sizeof(BinKey));
HexToBin is a generic function that converts a string like "1b34c38d" to
it''s binary equivalent. There is also a BinToHex which reverses the
process.
I know I could write a function to do it, but I threw all those routines
away 10 years ago when I started using Borland.
I am hoping that someone can help, because I really don''t want to have to
go back to writing all of those little conversion routines.
Hallo Fred!
I am trying to learn VC.NET and convert some Borland C++ applications. The
syntax differences are killing me...
Why? Its C++
Is there an easy way to convert a hex string, entered by the user, to a
binary string and back? This is the actual code I am trying to convert.
HexToBin(KeyEdit->Text.c_str(), BinKey, sizeof(BinKey));
HexToBin is a generic function that converts a string like "1b34c38d" to
it''s binary equivalent. There is also a BinToHex which reverses the
process.
I know I could write a function to do it, but I threw all those routines
away 10 years ago when I started using Borland.
Even than you should be able to program and learn! SCNR...
I am hoping that someone can help, because I really don''t want to have to
go back to writing all of those little conversion routines.
static inline TCHAR ByteToHex(int c)
{
return static_cast<TCHAR>((c<10) ? (c)+_T(''0'') : (c)-10+_T(''A''));
}
void BinToString(PCVOID pData, PTSTR pszOut, int iLen)
{
int iPos=0;
const BYTE *pszStr = (const BYTE *)pData;
while (iLen--)
{
BYTE byte = *pszStr++;
BYTE uByte = (byte & 0xF0) >> 4,
lByte = byte & 0xF;
pszOut[iPos++] = ByteToHex(uByte);
pszOut[iPos++] = ByteToHex(lByte);
}
pszOut[iPos] = ''\0'';
}
static inline int HexToByte(TCHAR c)
{
return (c>=_T(''0'') && c<=_T(''9'')) ? (c)-_T(''0'') :
(c>=_T(''A'') && c<=_T(''F'')) ? (c)-_T(''A'')+10 :
(c>=_T(''a'') && c<=_T(''f'')) ? (c)-_T(''a'')+10 : 0;
}
static inline bool IsHexDigit(TCHAR c)
{
return c>=_T(''0'') && c<=_T(''9'') ||
((c>=_T(''a'') && c<=_T(''f'')) ||
(c>=_T(''A'') && c<=_T(''F'')));
}
int StringToBin(PCTSTR pIn, PVOID pOut, int iLen)
{
if (iLen==-1)
iLen = _tcslen(pIn)/2;
memset(pOut,0,iLen);
if (!pIn)
return 0;
TCHAR uByte, lByte;
int iPos=0;
PBYTE pszStr = static_cast<PBYTE>(pOut);
while (iPos<iLen && IsHexDigit(uByte=pIn[0]) && IsHexDigit(lByte=pIn[1]))
{
*pszStr++ = static_cast<BYTE>(HexToByte(uByte)<<4 | HexToByte(lByte));
++iPos;
pIn += 2;
}
return *pIn ? 0 : iPos;
}
HTH
--
Martin Richter [MVP] WWJD
"In C we had to code our own bugs. In C++ we can inherit them."
FAQ : http://www.mpdvc.de
Samples: http://www.codeguru.com http://www.codeproject.com
这篇关于有更简单的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!