![]() |
Delphi-Version: 2010
Help translate from C
Code:
I don't understand what thes function are doing - convert character to ASCII code and back to character? Nope, I tried Ord/Chr and result was bad. WTF mean number + character statement :lol:?
int ctoi(char source)
{ /* Converts a character 0-9 to its equivalent integer value */ if((source >= '0') && (source <= '9')) return (source - '0'); return(source - 'A' + 10); } char itoc(int source) { /* Converts an integer value to its hexadecimal character */ if ((source >= 0) && (source <= 9)) { return ('0' + source); } else { return ('A' + (source - 10)); } } How to in Delphi? Please? :) |
AW: Help translate from C
Zitat:
Delphi-Quellcode:
.
Result := Ord(source) - Ord('A') + 10;
Den Rest solltest du selber Übersetzen können. Und den Grund für diese Brechnungen findet man, wenn man sich mal die ordinalen Werte eines Char ansieht. ![]() Ich nehme dafür aber gerne Array-Konstanten.
Delphi-Quellcode:
Ein kleiner Array-Zugriff ist irgendwie einfacher, als eine "komplizierte" Berechnung.
const ctoi: array['0'..'F'] of Byte = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15);
const itoc: array[0..15] of Char = '0123456789ABCDEF'; // oder const itoc: array[0..15] of Char = ('0', '1', '2', ..., 'E', 'F'); |
AW: Help translate from C
The purpose of the functions is given in the comments.
The character type in C is an integer type. Enough said. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:38 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz