NCBI C++ ToolKit
numeric_convert.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: numeric_convert.cpp 66530 2015-03-11 15:25:49Z ucko $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Vladimir Soussov
27  *
28  * File Description: Numeric conversions
29  *
30  */
31 
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbistd.hpp>
35 #include <string>
36 #include <stdio.h>
37 
38 
40 
41 
42 #define MAXPRECISION 50
43 
44 
45 static int s_NumericBytesPerPrec[] =
46 {2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, 9, 9, 9,
47  10, 10, 11, 11, 11, 12, 12, 13, 13, 14, 14, 14, 15, 15,
48  16, 16, 16, 17, 17, 18, 18, 19, 19, 19, 20, 20, 21, 21, 21,
49  22, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26};
50 
51 
53 unsigned char* longlong_to_numeric (Int8 l_num, unsigned int prec, unsigned char* cs_num)
54 {
55  bool needs_del= false;
56 
57  if(prec == 0) return 0;
58 
59  if (cs_num == 0) {
60  cs_num= new unsigned char[MAXPRECISION];
61  needs_del= true;
62  }
63  memset (cs_num, 0, prec);
64 
65  int BYTE_NUM = s_NumericBytesPerPrec[prec-1];
66  unsigned char* number = &cs_num[BYTE_NUM - 1];
67  if (l_num != 0) {
68  if (l_num < 0) {
69  l_num *= (-1);
70  cs_num[0] = 0x1;
71  }
72  while (l_num != 0 && number >= cs_num) {
73  Int8 rem = l_num%256;
74  *number = (unsigned char)rem;
75  l_num = l_num/256;
76  number--;
77  if (number <= cs_num) {
78  if (needs_del) delete[] cs_num;
79  return 0;
80  }
81  }
82  }
83  return cs_num;
84 
85 }
86 
87 
89 Int8 numeric_to_longlong(unsigned int precision, unsigned char* cs_num)
90 
91 {
92 
93  if(precision == 0) return 0;
94 
95  int BYTE_NUM = s_NumericBytesPerPrec[precision - 1];
96  Int8 my_long = 0;
97 
98  for (int i = 1; i < BYTE_NUM; i++) {
99  if (my_long > kMax_I8 >> 8) {
100  return 0;
101  }
102  my_long = my_long*256 + cs_num[i];
103  }
104  if (cs_num[0] != 0) {
105  my_long*= -1;
106  }
107 
108  return my_long;
109 }
110 
111 
113 void swap_numeric_endian(unsigned int precision, unsigned char* num)
114 {
115  if(precision == 0) return;
116 
117  int BYTE_NUM= s_NumericBytesPerPrec[precision - 1] - 1;
118  unsigned char c;
119  int i, j;
120 
121  for(i= 0, j= BYTE_NUM-1; i < j; i++, j--) {
122  c= num[i];
123  num[i]= num[j];
124  num[j]= c;
125  }
126 }
127 
128 
130 
Include a standard set of the NCBI C++ Toolkit most basic headers.
static char precision
Definition: genparams.c:28
#define kMax_I8
Definition: ncbi_limits.h:221
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define NCBI_DBAPIDRIVER_EXPORT
Definition: ncbi_export.h:392
int i
unsigned char * longlong_to_numeric(Int8 l_num, unsigned int prec, unsigned char *cs_num)
static int s_NumericBytesPerPrec[]
Int8 numeric_to_longlong(unsigned int precision, unsigned char *cs_num)
void swap_numeric_endian(unsigned int precision, unsigned char *num)
#define MAXPRECISION
static BOOL number
Definition: pcregrep.c:193
Modified on Thu May 02 14:35:51 2024 by modify_doxy.py rev. 669887