回答編集履歴

2

ソース追記

2018/11/18 03:16

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -69,3 +69,55 @@
69
69
  ```
70
70
 
71
71
  usr~/test/cpp %
72
+
73
+
74
+
75
+ [sprintf]
76
+
77
+ ```cpp
78
+
79
+ usr~/test/cpp % cat c1016s.cpp
80
+
81
+ #include <iostream>
82
+
83
+
84
+
85
+ using namespace std;
86
+
87
+
88
+
89
+ int main(void)
90
+
91
+ {
92
+
93
+ int x;
94
+
95
+ char buf[16];
96
+
97
+ //
98
+
99
+ cin >> x;
100
+
101
+ //
102
+
103
+ sprintf(buf,"%X",x);
104
+
105
+ //
106
+
107
+ cout << buf << endl;
108
+
109
+
110
+
111
+ return 0;
112
+
113
+ }
114
+
115
+ usr~/test/cpp % ./a.out
116
+
117
+ 147
118
+
119
+ 93
120
+
121
+ usr~/test/cpp %
122
+
123
+ ```

1

ソース追記

2018/11/18 03:16

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -7,3 +7,65 @@
7
7
 
8
8
 
9
9
  intの値を16進文字列にするならsprintf(buf,"%x",v)で出来ます。
10
+
11
+ 「追記」
12
+
13
+ プログラムでするなら・・・
14
+
15
+ usr~/test/cpp % ./a.out
16
+
17
+ 456
18
+
19
+ 1C8
20
+
21
+ usr~/test/cpp % cat c1016.cpp
22
+
23
+ ```cpp
24
+
25
+ #include <iostream>
26
+
27
+
28
+
29
+ using namespace std;
30
+
31
+
32
+
33
+ int main(void)
34
+
35
+ {
36
+
37
+ static const char hex[]= "0123456789ABCDEF";
38
+
39
+ int x;
40
+
41
+ char buf[16];
42
+
43
+ char *ptr=&buf[10];
44
+
45
+ //
46
+
47
+ cin >> x;
48
+
49
+ //
50
+
51
+ *ptr-- = '\0';
52
+
53
+ do{
54
+
55
+ *ptr-- = hex[x & 0xf];
56
+
57
+ }while( x >>= 4 );
58
+
59
+
60
+
61
+ cout << ++ptr << endl;
62
+
63
+
64
+
65
+ return 0;
66
+
67
+ }
68
+
69
+ ```
70
+
71
+ usr~/test/cpp %