我正在用 Delphi 7维护一个旧项目。我需要将一个长的十六进制字符串转换为十进制字符串。我在C#中搜索并找到了示例代码,但在Delphi中没有。我只有两个选择:
1.在 Delphi 7中实现或使用函数。
1.在 Delphi 2010中实现或使用一个函数,然后将其导出为DLL。
我正在处理的十六进制字符串的最大长度是40个字符,下面是一个示例:
'6F000080B4D4B3426C66A655010001000080B4'
我使用rapidtables进行转换,这里是输出
'2475382888117010136950089026926167642744062132'
我希望有人以前解决过这个问题,可以帮助我。也许有人给予我一个算法,我可以用来写一个函数在 Delphi 中。
注意事项:
在 Delphi 中,Int64的最大正值是$7FFFFFFFFFFFFFFF
= 9223372036854775807
,这个值远远不是我所需要的。
1条答案
按热度按时间798qvoo81#
For this to solve we need to cut it down into three steps:
As tiny helpers which are needed in the other functions let me have these:
Addition
We all learnt written addition in school: write all numbers in one row, then add each row's digits and carry that sum's additional digits over to the next row of digits of the summands. This can also be done easily:
I did not restrict it to always 2 summands for the following reasons:
$30
is the ASCII code for the character'0'
- subtracting the potential character'0'
to'9'
by that of'0'
gives us the value0
to9
.Multiplication
We all learnt written multiplication in school, too: for each digit of one factor calculate that product (which can only be an "easy" multiplication by 0 to 9), write down all those products in a row as per digit position (optionally with trailing zeroes), then add all those products into a sum (referencing written addition). This can also be done easily, since we now have solved addition already:
It could have been even shorter, since
Summe()
can already deal with 0 and 1 summands - I don't really need to treat that differently. As previously told: the easy multiplication is done by simple addition - not very performance efficient, but easy to comprehend.Hexadecimal to decimal conversion
Since we can now add and multiply, we're also able to convert each digit of a non-decimal-base number and increase the outcome to the overall result:
It even works not only for hexadecimal (base 16) input, but also for others.
$41
is the ASCII value for'A'
- subtracting the potential characters'A'
to'Z'
by that of'A'
gives us the value0
to25
, to which we then just add10
.Tests
Strings are implied. Spaces are just for optical reasons.
| function | parameters | result | proof |
| ------------ | ------------ | ------------ | ------------ |
|
Summe()
| 123 + 456 | 579 | brain ||
Summe()
| 123 + 456 + 9999 | 10578 | MS calc, brain ||
Produkt()
| 36 * 12 | 504 | MS calc, brain ||
Produkt()
| 8 6426578999 * 9731421999 | 8 4105351216 9179999001 | rapidtables.com ||
ConvertToDecimal()
| 6F0000 80B4D4B3 426C66A6 55010001 000080B4 | 247538 2888117010 1369500890 2692616764 2744062132 | rapidtables.com |Summary
0
, but a result of0
is not reduced to an empty String either.AnsiString
,UnicodeString
,WideString
and so on. However,Char
is used as type, which is bound to that choice. But the functionsChr()
andOrd()
should support everything again.