N
Nathan Waddington
Hello Everyone,
My program receives a string representation of a 64bit hex number that
needs to be displayed as the decimal number it represents. The compiler
that is being used is Dynamic C and (alas) it does not support 64bit
numbers.
So far i have been able to take the least significant 4 bytes and
convert them correctly, but when i try to take the most significant 4
bytes i am unable to convert them correctly. Below are the 3 functions
that i am using, the third is the one that i am having trouble with
(the others are here as a reference).
Can anyone provide any suggestions as to how i can deal with these?
here is some sample output:
original: 2EEA9
expected: 00192169
actual: 00192169
original: 5F5E0FF
expected: 99999999
actual: 99999999
original: 1C6BF526340003
expected: 8000000000000003
actual: 640942083
Thanks in advance.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
unsigned int UTIL_HexCharToInt(char);
unsigned long UTIL_HexStrToLong(char *, int);
void DecodeAcct(char *, char *);
unsigned int UTIL_HexCharToInt(char c)
{
auto unsigned int lResult;
lResult = 0L;
if (isxdigit(c)) {
if (c <= '9') {
lResult = (unsigned int) (c - '0');
} else {
c = toupper(c);
lResult = (unsigned int) (c - 'A') + 10;
}
}
return (lResult);
}
unsigned long UTIL_HexStrToLong(char *szStr, int len)
{
auto unsigned long lResult, lTmp, lPwr;
auto int i;
auto char *pStr;
// work from the end of the buffer
pStr = &szStr[len - 1];
// convert the hex string to long
for (i = 0, lResult = 0L; i < len; i++, pStr--) {
// work out power of the hex digit
switch (i) {
case 0:
lPwr = 1L;
break;
case 1:
lPwr = 16L;
break;
default:
lPwr = lPwr * 16L;
break;
}
// convert the hex character to long
lTmp = (unsigned long) UTIL_HexCharToInt(*pStr);
if (lTmp) {
lResult += lTmp * lPwr;
}
}
// return hex string in unsigned long
return (lResult);
}
void DecodeAcct(char *In, char *Out)
{
#define OVERFLOW 8
auto char overflow[9], temp[9];
auto int i, diff;
auto unsigned long int ln;
if (strlen(In) >= OVERFLOW+10) {
ln = 0;
diff = strlen(In) - OVERFLOW;
memset(overflow, 0, sizeof(overflow));
/* move most significant bytes into overflow buffer */
for (i = 0; i < diff; i++) {
overflow = In;
}
/* remove extra from original string */
for (i = 0; In != 0 && i <= strlen(In); i++) {
In = In[i + diff];
}
ln = UTIL_HexStrToLong(overflow, strlen(overflow));
sprintf(Out, "%lu", ln);
}
ln = 0;
memset(temp, 0, sizeof(temp));
ln = UTIL_HexStrToLong(In, strlen(In));
sprintf(temp, "%08lu", ln);
strcat(Out, temp);
}
My program receives a string representation of a 64bit hex number that
needs to be displayed as the decimal number it represents. The compiler
that is being used is Dynamic C and (alas) it does not support 64bit
numbers.
So far i have been able to take the least significant 4 bytes and
convert them correctly, but when i try to take the most significant 4
bytes i am unable to convert them correctly. Below are the 3 functions
that i am using, the third is the one that i am having trouble with
(the others are here as a reference).
Can anyone provide any suggestions as to how i can deal with these?
here is some sample output:
original: 2EEA9
expected: 00192169
actual: 00192169
original: 5F5E0FF
expected: 99999999
actual: 99999999
original: 1C6BF526340003
expected: 8000000000000003
actual: 640942083
Thanks in advance.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
unsigned int UTIL_HexCharToInt(char);
unsigned long UTIL_HexStrToLong(char *, int);
void DecodeAcct(char *, char *);
unsigned int UTIL_HexCharToInt(char c)
{
auto unsigned int lResult;
lResult = 0L;
if (isxdigit(c)) {
if (c <= '9') {
lResult = (unsigned int) (c - '0');
} else {
c = toupper(c);
lResult = (unsigned int) (c - 'A') + 10;
}
}
return (lResult);
}
unsigned long UTIL_HexStrToLong(char *szStr, int len)
{
auto unsigned long lResult, lTmp, lPwr;
auto int i;
auto char *pStr;
// work from the end of the buffer
pStr = &szStr[len - 1];
// convert the hex string to long
for (i = 0, lResult = 0L; i < len; i++, pStr--) {
// work out power of the hex digit
switch (i) {
case 0:
lPwr = 1L;
break;
case 1:
lPwr = 16L;
break;
default:
lPwr = lPwr * 16L;
break;
}
// convert the hex character to long
lTmp = (unsigned long) UTIL_HexCharToInt(*pStr);
if (lTmp) {
lResult += lTmp * lPwr;
}
}
// return hex string in unsigned long
return (lResult);
}
void DecodeAcct(char *In, char *Out)
{
#define OVERFLOW 8
auto char overflow[9], temp[9];
auto int i, diff;
auto unsigned long int ln;
if (strlen(In) >= OVERFLOW+10) {
ln = 0;
diff = strlen(In) - OVERFLOW;
memset(overflow, 0, sizeof(overflow));
/* move most significant bytes into overflow buffer */
for (i = 0; i < diff; i++) {
overflow = In;
}
/* remove extra from original string */
for (i = 0; In != 0 && i <= strlen(In); i++) {
In = In[i + diff];
}
ln = UTIL_HexStrToLong(overflow, strlen(overflow));
sprintf(Out, "%lu", ln);
}
ln = 0;
memset(temp, 0, sizeof(temp));
ln = UTIL_HexStrToLong(In, strlen(In));
sprintf(temp, "%08lu", ln);
strcat(Out, temp);
}