473,406 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Displaying 64bit Numbers

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[i] = In[i];
}

/* remove extra from original string */
for (i = 0; In[i] != 0 && i <= strlen(In); i++) {
In[i] = 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);
}

--
Nathan Waddington
Email: nw*********@parking.ca

Nov 13 '05 #1
8 5640
Sorry, wanted to make a correction to the Function:
if (strlen(In) >= OVERFLOW+10) { should read: if (strlen(In) >= OVERFLOW) {

Nathan Waddington wrote: 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[i] = In[i];
}

/* remove extra from original string */
for (i = 0; In[i] != 0 && i <= strlen(In); i++) {
In[i] = 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);
}


--
Nathan Waddington
Email: nw*********@parking.ca

Nov 13 '05 #2

"Nathan Waddington" <nw*********@parking.ca> wrote in message

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.

I found this surprisingly hard, when you can't hold the number in
intermediate binary.

Here's what I came up with (tested, but not exhaustively).

/*
convenience function to duplicate a string
*/
char *mystrdup(const char *str)
{
char *answer;

answer = malloc(strlen(str) + 1);

/* you will have to decide your out of memory strategy */
if(!answer)
exit(EXIT_FAILURE);

strcpy(answer, str);
return answer;
}

/*
convert a hex digit to binary
*/
int hexval(char hex)
{
static char *digits = "0123456789ABCDEF";
char *pos;

hex = toupper(hex);
pos = strchr(digits, hex);
assert(pos);

return pos - digits;
}

/*
convert a binary number 0-16 to a hex digit
*/
char bintohex(int val)
{
static char *digits = "0123456789ABCDEF";

assert(val >= 0 && val < 16);
return digits[val];
}

/*
divide a hex number by ten, in hexadecimal
out - return pointer for answer (in hex);
hex - input number
returns - the remainder
*/
int divideby10(char *out, const char *hex)
{
int i = 0;
int num;
int carry = 0;
char *ptr;
int N = 0;

/* this does the division */
while(hex[i])
{
num = carry * 16 + hexval(hex[i++]);
out[N++] = bintohex(num/10);
carry = num % 10;
}
out[N] = 0;

/* remove all leading zeros */
ptr = out;
while(*ptr == '0')
ptr++;
memmove(out, ptr, N + ptr - out);
/* if the number is zero, represent it as a single zero */
if(*out == 0)
strcpy(out, "0");

return carry;
}

/*
function to convert an arbitrary hex string to decimal
Params: dec - return pointer for the decimal number
hex - input pointer to the hex number (no leading 0x)

*/
void hextodecimal(char *dec, const char *hex)
{
char *buff1;
char *buff2;
char temp;
int dig;
int N = 0;
int i;

/* use of the buffers allows us to handle any length of hex */
buff1 = mystrdup(hex);
buff2 = mystrdup(hex);

/* this is a simple algorithm. Divide by ten repeatedly until
the hex number is reduced to zero */
do
{
dig = divideby10(buff2, buff1);
dec[N++] = dig + '0';
strcpy(buff1, buff2);
}
while(strcmp(buff1, "0"));

/* add the trailing NUL */
dec[N] = 0;

/* the decimal number is reversed, so put it right */
for(i=0;i<N/2;i++)
{
temp = dec[i];
dec[i] = dec[N-i-1];
dec[N-i-1] = temp;
}

/* don't forget to free our resources */
free(buff1);
free(buff2);

}

Hope this is of use to you.

Nov 13 '05 #3
Malcolm wrote:

Here's what I came up with (tested, but not exhaustively).
Hope this is of use to you.


Thank you, it very much is.

--
Nathan Waddington
Email: nw*********@parking.ca

Nov 13 '05 #4
Nathan Waddington <nw*********@parking.ca> wrote in message news:<C9***********************@news2.calgary.shaw .ca>...
Malcolm wrote:

Here's what I came up with (tested, but not exhaustively).
Hope this is of use to you.


Thank you, it very much is.


Use "%I64d" or "%I64u" or "%I64x" in the printf statement to
display 64Bit results. This works on Bloodshed C++ and on MS Visual
C++ . I tested it out.
I found the info from an old posting in 2001.

Bill Hanna
Nov 13 '05 #5
On Wed,23 Jul 2003,"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote:
int divideby10(char *out, const char *hex)
{
int i = 0;
int num;
int carry = 0;
char *ptr;
int N = 0;

/* this does the division */
while(hex[i])
{
num = carry * 16 + hexval(hex[i++]);
out[N++] = bintohex(num/10);
carry = num % 10;
}
out[N] = 0;

/* remove all leading zeros */
ptr = out;
while(*ptr == '0')
ptr++;
memmove(out, ptr, N + ptr - out);


Why not memmove(out, ptr, N - (ptr - out) + 1 ); ?

Nov 13 '05 #6

"Giuseppe" <gi******@giuseppe.wwwew> wrote in message
> memmove(out, ptr, N + ptr - out);


Why not memmove(out, ptr, N - (ptr - out) + 1 ); ?

Whoops, yes this is a potential buffer overflow.
Nov 13 '05 #7
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message news:<bf**********@news7.svr.pol.co.uk>...
"Nathan Waddington" <nw*********@parking.ca> wrote in message

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.

I found this surprisingly hard, when you can't hold the number in
intermediate binary.

Here's what I came up with (tested, but not exhaustively).

/*
convenience function to duplicate a string
*/
char *mystrdup(const char *str)
{
char *answer;

answer = malloc(strlen(str) + 1);

/* you will have to decide your out of memory strategy */
if(!answer)
exit(EXIT_FAILURE);

strcpy(answer, str);
return answer;
}

/*
convert a hex digit to binary
*/
int hexval(char hex)
{
static char *digits = "0123456789ABCDEF";
char *pos;

hex = toupper(hex);
pos = strchr(digits, hex);
assert(pos);

return pos - digits;
}

/*
convert a binary number 0-16 to a hex digit
*/
char bintohex(int val)
{
static char *digits = "0123456789ABCDEF";

assert(val >= 0 && val < 16);
return digits[val];
}

/*
divide a hex number by ten, in hexadecimal
out - return pointer for answer (in hex);
hex - input number
returns - the remainder
*/
int divideby10(char *out, const char *hex)
{
int i = 0;
int num;
int carry = 0;
char *ptr;
int N = 0;

/* this does the division */
while(hex[i])
{
num = carry * 16 + hexval(hex[i++]);
out[N++] = bintohex(num/10);
carry = num % 10;
}
out[N] = 0;

/* remove all leading zeros */
ptr = out;
while(*ptr == '0')
ptr++;
memmove(out, ptr, N + ptr - out);
/* if the number is zero, represent it as a single zero */
if(*out == 0)
strcpy(out, "0");

return carry;
}

/*
function to convert an arbitrary hex string to decimal
Params: dec - return pointer for the decimal number
hex - input pointer to the hex number (no leading 0x)

*/
void hextodecimal(char *dec, const char *hex)
{
char *buff1;
char *buff2;
char temp;
int dig;
int N = 0;
int i;

/* use of the buffers allows us to handle any length of hex */
buff1 = mystrdup(hex);
buff2 = mystrdup(hex);

/* this is a simple algorithm. Divide by ten repeatedly until
the hex number is reduced to zero */
do
{
dig = divideby10(buff2, buff1);
dec[N++] = dig + '0';
strcpy(buff1, buff2);
}
while(strcmp(buff1, "0"));

/* add the trailing NUL */
dec[N] = 0;

/* the decimal number is reversed, so put it right */
for(i=0;i<N/2;i++)
{
temp = dec[i];
dec[i] = dec[N-i-1];
dec[N-i-1] = temp;
}

/* don't forget to free our resources */
free(buff1);
free(buff2);

}

Hope this is of use to you.


Hi Malcom,

this is exactly what I was searching for. I needed a string based
conversion routine for a language called SMALL. As this (C derived)
language only supports 32Bit integers your routine is just what i
need.

Thx, Gerald
Nov 13 '05 #8

"Gerald S." <st**@skidata.com> wrote in message
/* remove all leading zeros */
ptr = out;
while(*ptr == '0')
ptr++;
memmove(out, ptr, N + ptr - out);

should be
memmove(out, ptr, N - (ptr - out) + 1 );

as corrected by Giuseppe
Hi Malcom,

this is exactly what I was searching for. I needed a string based
conversion routine for a language called SMALL. As this (C derived)
language only supports 32Bit integers your routine is just what i
need.

Nov 13 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christian McArdle | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.os.ms-windows.programmer.64bit This is a formal Request For Discussion (RFD) to create comp.os.ms-windows.programmer.64bit as an unmoderated...
7
by: Jeff Gilchrist | last post by:
I have tried searching the newsgroup along with the GCC site and could not find what I think is probably a simple solution. I am using a 64bit unsigned long long integer and can manipulate 64bits...
2
by: Rose | last post by:
Hi all, I'm a newbie to C# and .NET, so please be gentle! My limited background is in VBScript,ASP, and Access dbs. I need to retrieve data from a SQL server db (8.0) to be displayed in a...
11
by: Vijay Chegu | last post by:
I have built a 64bit ATL COM dll. When i register the dll on IA64 windows Enterprise server 2003, i get following error. DllRegisterServer in mydll.dll failed. Return code was : 0x80020009 ...
3
by: Frank Uray | last post by:
Hi all I have written a WindowsService in C# 2005. I am running this service on a 64bit Windows Server 2003 and I like to run some Interop.Excel within this service. It seams that Interop...
4
by: mike | last post by:
I have found that orig tested 64 bit on our 64bit windows 2003 server...about 1 year ago...and company decided to use sql 32 bit on the 64bit os my question and any information is very welcome ...
19
by: llothar | last post by:
I must say i didn't expect this. I just did some measures on FreeBSD 6.2 with gcc 3.4.6 and there is absolutely no significant difference between 32 and 64 bit mode - neither in compilation speed,...
1
by: GaryDean | last post by:
We have been developing all of our .net applications on 32 bit windows using 32 bit SQL Server. We are being asked to now deploy to servers running 64bit windows and 64bit SQL Server. Are there...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.