📄 rv64ascii.c
字号:
/***********************************************************************
Filename : rv64ascii.c
Description: functions for converting 64 bit numbers to ASCII and back
************************************************************************
Copyright (c) 2001 RADVISION Inc. and RADVISION Ltd.
************************************************************************
NOTICE:
This document contains information that is confidential and proprietary
to RADVISION Inc. and RADVISION Ltd.. No part of this document may be
reproduced in any form whatsoever without written prior approval by
RADVISION Inc. or RADVISION Ltd..
RADVISION Inc. and RADVISION Ltd. reserve the right to revise this
publication and make changes without obligation to notify any person of
such revisions or changes.
***********************************************************************/
#include "rv64ascii.h"
#include "rvansi.h"
#include <string.h>
/* These functions Deal with differences and bugs in printing 64 bit numbers. */
RvStatus Rv64AsciiInit(void)
{
return RV_OK;
}
RvStatus Rv64AsciiEnd(void)
{
return RV_OK;
}
/* Convert an unsigned 64 bit number to string. The buf parameter */
/* should be at least 22 bytes or an overrun can occur. */
RvChar *Rv64UtoA(RvChar *buf, RvUint64 num64)
{
#if (RV_64TOASCII_TYPE == RV_64TOASCII_MANUAL)
RvChar *cptr, tmpbuf[RV_64TOASCII_BUFSIZE];
RvInt32 temp;
RvSize_t size;
#endif
#if defined(RV_NULLCHECK)
if(buf == NULL) return NULL;
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_STANDARD)
RvSprintf(buf, "%llu", num64);
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_WIN32)
RvSprintf(buf, "%I64u", num64);
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_MANUAL)
/* sprintf is broken, have to do it manually */
cptr = &tmpbuf[RV_64TOASCII_BUFSIZE - 1];
*cptr = '\0';
size = 1;
do{
cptr -= 1;
temp = (RvInt32)Rv64Modulu(num64, RvUint64Const(10));
num64 = Rv64Divide(num64, RvUint64Const(10));
*cptr = '0' + (RvChar)temp;
size++;
} while(num64 > RvUint64Const(0));
memcpy(buf, cptr, size);
#endif
return buf;
}
/* Convert a signed 64 bit number to string. The buf parameter */
/* should be at least 22 bytes or an overrun can occur. */
RVCOREAPI RvChar * RVCALLCONV Rv64toA(RvChar *buf, RvInt64 num64)
{
#if (RV_64TOASCII_TYPE == RV_64TOASCII_MANUAL)
RvChar *cptr;
RvUint64 tmpnum64;
#endif
#if defined(RV_NULLCHECK)
if(buf == NULL) return NULL;
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_STANDARD)
RvSprintf(buf, "%lld", num64);
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_WIN32)
RvSprintf(buf, "%I64d", num64);
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_MANUAL)
/* sprintf is broken, have to do it manually */
/* Put the sign in and send the rest as unsigned */
cptr = buf;
if(num64 < RvInt64Const(0)) {
*cptr = '-';
num64 *= RvInt64Const(-1);
} else *cptr = ' ';
cptr += 1;
tmpnum64 = (RvUint64)num64;
Rv64UtoA(cptr, tmpnum64);
#endif
return buf;
}
/* Convert an unsigned 64 bit number to a hex string. The buf */
/* parameter should be at least 18 bytes or an overrun can occur. */
RvChar *Rv64UtoHex(RvChar *buf, RvUint64 num64)
{
#if (RV_64TOASCII_TYPE == RV_64TOASCII_MANUAL)
RvUint32 msb, lsb;
int shiftnum;
#endif
#if defined(RV_NULLCHECK)
if(buf == NULL) return NULL;
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_STANDARD)
RvSprintf(buf, "%llx", num64);
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_WIN32)
RvSprintf(buf, "%I64x", num64);
#endif
#if (RV_64TOASCII_TYPE == RV_64TOASCII_MANUAL)
/* sprintf is broken, have to do it in pieces */
shiftnum = 32; /* compiler workaround */
msb = (RvUint32)(num64 >> shiftnum);
lsb = (RvUint32)(num64);
if(msb > 0) {
RvSprintf(buf, "%x%08x", msb, lsb);
} else RvSprintf(buf,"%x",lsb);
#endif
return buf;
}
#if defined(RV_TEST_CODE)
#include "rvstdio.h"
#define TESTVAL1 RvInt64Const(288230376151711744)
#define TESTVAL2 RvInt64Const(-41175768021673106)
#define TESTVAL3 RvUint64Const(14757395258967641292)
#define TESTVAL4 RvInt64Const(475)
#define TESTVAL5 RvUint64Const(0x5a5a5a0001)
void Rv64AsciiTest(void) {
RvStatus result;
RvInt64 testval;
RvUint64 utestval;
RvChar buf[RV_64TOASCII_BUFSIZE];
RvPrintf("Starting test of rv64ascii.\n");
RvPrintf("Rv64AsciiInit: ");
result = Rv64AsciiInit();
if(result != RV_OK) {
RvPrintf("ERROR %d\n", result);
} else RvPrintf("OK\n");
RvPrintf("TESTVAL1:\n");
testval = TESTVAL1;
utestval = TESTVAL1;
Rv64toA(buf, testval);
RvPrintf(" Rv64toA = %s\n",buf);
Rv64UtoA(buf, utestval);
RvPrintf(" Rv64UtoA = %s\n",buf);
Rv64UtoHex(buf, utestval);
RvPrintf(" Rv64UtoHex = %s\n",buf);
RvPrintf("TESTVAL2:\n");
testval = TESTVAL2;
utestval = (RvUint64)TESTVAL2;
Rv64toA(buf, testval);
RvPrintf(" Rv64toA = %s\n",buf);
Rv64UtoA(buf, utestval);
RvPrintf(" Rv64UtoA = %s\n",buf);
Rv64UtoHex(buf, utestval);
RvPrintf(" Rv64UtoHex = %s\n",buf);
RvPrintf("TESTVAL3:\n");
testval = (RvInt64)TESTVAL3;
utestval = (RvUint64)TESTVAL3;
Rv64toA(buf, testval);
RvPrintf(" Rv64toA = %s\n",buf);
Rv64UtoA(buf, utestval);
RvPrintf(" Rv64UtoA = %s\n",buf);
Rv64UtoHex(buf, utestval);
RvPrintf(" Rv64UtoHex = %s\n",buf);
RvPrintf("TESTVAL4:\n");
testval = TESTVAL4;
utestval = TESTVAL4;
Rv64toA(buf, testval);
RvPrintf(" Rv64toA = %s\n",buf);
Rv64UtoA(buf, utestval);
RvPrintf(" Rv64UtoA = %s\n",buf);
Rv64UtoHex(buf, utestval);
RvPrintf(" Rv64UtoHex = %s\n",buf);
RvPrintf("TESTVAL5:\n");
testval = TESTVAL5;
utestval = TESTVAL5;
Rv64toA(buf, testval);
RvPrintf(" Rv64toA = %s\n",buf);
Rv64UtoA(buf, utestval);
RvPrintf(" Rv64UtoA = %s\n",buf);
Rv64UtoHex(buf, utestval);
RvPrintf(" Rv64UtoHex = %s\n",buf);
RvPrintf("Rv64AsciiEnd: ");
result = Rv64AsciiEnd();
if(result != RV_OK) {
RvPrintf("ERROR %d\n", result);
} else RvPrintf("OK\n");
}
#endif /* RV_TEST_CODE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -