1//============================================================================
2//
3// SSSS tt lll lll
4// SS SS tt ll ll
5// SS tttttt eeee ll ll aaaa
6// SSSS tt ee ee ll ll aa
7// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
8// SS SS tt ee ll ll aa aa
9// SSSS ttt eeeee llll llll aaaaa
10//
11// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
12// and the Stella Team
13//
14// See the file "License.txt" for information on usage and redistribution of
15// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16//============================================================================
17
18#include "Base.hxx"
19
20namespace Common {
21
22// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
23string Base::toString(int value, Common::Base::Format outputBase)
24{
25 static char vToS_buf[32];
26
27 if(outputBase == Base::F_DEFAULT)
28 outputBase = myDefaultBase;
29
30 switch(outputBase)
31 {
32 case Base::F_2: // base 2: 8 or 16 bits (depending on value)
33 case Base::F_2_8: // base 2: 1 byte (8 bits) wide
34 case Base::F_2_16: // base 2: 2 bytes (16 bits) wide
35 {
36 int places = (outputBase == Base::F_2_8 ||
37 (outputBase == Base::F_2 && value < 0x100)) ? 8 : 16;
38 vToS_buf[places] = '\0';
39 int bit = 1;
40 while(--places >= 0) {
41 if(value & bit) vToS_buf[places] = '1';
42 else vToS_buf[places] = '0';
43 bit <<= 1;
44 }
45 break;
46 }
47
48 case Base::F_10: // base 10: 3 or 5 bytes (depending on value)
49 if(value > -0x100 && value < 0x100)
50 std::snprintf(vToS_buf, 5, "%3d", Int16(value));
51 else
52 std::snprintf(vToS_buf, 6, "%5d", value);
53 break;
54
55 case Base::F_10_02: // base 10: 2 digits (with leading zero)
56 std::snprintf(vToS_buf, 3, "%02d", value);
57 break;
58
59 case Base::F_10_3: // base 10: 3 digits
60 std::snprintf(vToS_buf, 4, "%3d", value);
61 break;
62
63 case Base::F_10_4: // base 10: 4 digits
64 std::snprintf(vToS_buf, 5, "%4d", value);
65 break;
66
67 case Base::F_10_5: // base 10: 5 digits
68 std::snprintf(vToS_buf, 6, "%5d", value);
69 break;
70
71 case Base::F_16_1: // base 16: 1 byte wide
72 std::snprintf(vToS_buf, 2, hexUppercase() ? "%1X" : "%1x", value);
73 break;
74 case Base::F_16_2: // base 16: 2 bytes wide
75 std::snprintf(vToS_buf, 3, hexUppercase() ? "%02X" : "%02x", value);
76 break;
77 case Base::F_16_2_2:
78 std::snprintf(vToS_buf, 6, hexUppercase() ? "%02X.%02X" : "%02x.%02x",
79 value >> 8, value & 0xff );
80 break;
81 case Base::F_16_3_2:
82 std::snprintf(vToS_buf, 7, hexUppercase() ? "%03X.%02X" : "%03x.%02x",
83 value >> 8, value & 0xff );
84 break;
85 case Base::F_16_4: // base 16: 4 bytes wide
86 std::snprintf(vToS_buf, 5, hexUppercase() ? "%04X" : "%04x", value);
87 break;
88 case Base::F_16_8: // base 16: 8 bytes wide
89 std::snprintf(vToS_buf, 9, hexUppercase() ? "%08X" : "%08x", value);
90 break;
91
92 case Base::F_16: // base 16: 2, 4, 8 bytes (depending on value)
93 default:
94 if(value < 0x100)
95 std::snprintf(vToS_buf, 3, hexUppercase() ? "%02X" : "%02x", value);
96 else if(value < 0x10000)
97 std::snprintf(vToS_buf, 5, hexUppercase() ? "%04X" : "%04x", value);
98 else
99 std::snprintf(vToS_buf, 9, hexUppercase() ? "%08X" : "%08x", value);
100 break;
101 }
102
103 return string(vToS_buf);
104}
105
106// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107Base::Format Base::myDefaultBase = Base::F_16;
108
109// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
110std::ios_base::fmtflags Base::myHexflags = std::ios_base::hex;
111
112} // Namespace Common
113