teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

ソース追記

2018/11/18 03:16

投稿

cateye
cateye

スコア6851

answer CHANGED
@@ -33,4 +33,30 @@
33
33
  return 0;
34
34
  }
35
35
  ```
36
- usr~/test/cpp %
36
+ usr~/test/cpp %
37
+
38
+ [sprintf]
39
+ ```cpp
40
+ usr~/test/cpp % cat c1016s.cpp
41
+ #include <iostream>
42
+
43
+ using namespace std;
44
+
45
+ int main(void)
46
+ {
47
+ int x;
48
+ char buf[16];
49
+ //
50
+ cin >> x;
51
+ //
52
+ sprintf(buf,"%X",x);
53
+ //
54
+ cout << buf << endl;
55
+
56
+ return 0;
57
+ }
58
+ usr~/test/cpp % ./a.out
59
+ 147
60
+ 93
61
+ usr~/test/cpp %
62
+ ```

1

ソース追記

2018/11/18 03:16

投稿

cateye
cateye

スコア6851

answer CHANGED
@@ -2,4 +2,35 @@
2
2
 
3
3
  よく意味がわからないのですが、PCの中では10進も16進も(ただのバイナリデータで)変わりません。
4
4
 
5
- intの値を16進文字列にするならsprintf(buf,"%x",v)で出来ます。
5
+ intの値を16進文字列にするならsprintf(buf,"%x",v)で出来ます。
6
+ 「追記」
7
+ プログラムでするなら・・・
8
+ usr~/test/cpp % ./a.out
9
+ 456
10
+ 1C8
11
+ usr~/test/cpp % cat c1016.cpp
12
+ ```cpp
13
+ #include <iostream>
14
+
15
+ using namespace std;
16
+
17
+ int main(void)
18
+ {
19
+ static const char hex[]= "0123456789ABCDEF";
20
+ int x;
21
+ char buf[16];
22
+ char *ptr=&buf[10];
23
+ //
24
+ cin >> x;
25
+ //
26
+ *ptr-- = '\0';
27
+ do{
28
+ *ptr-- = hex[x & 0xf];
29
+ }while( x >>= 4 );
30
+
31
+ cout << ++ptr << endl;
32
+
33
+ return 0;
34
+ }
35
+ ```
36
+ usr~/test/cpp %