Ahmed Nageeb Ahmed Nageeb - 1 month ago 7
C++ Question

cant convert ascii charcters to int and hex at the same time

i have aproblem converting ascii charcters to int and hex at the same time

#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
int main()
{


for (int i=0;i<265;i++){
cout<<char(i)<<" ";
cout<<int(i)<<endl;
cout << hex<< i<<endl ;
}


here is the result

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

a a
b b
c c
d d
e e
f f
 10 10
11 11
12 12
 13 13
14 14
15 15
16 16
17 17
18 18
19 19
1a 1a
1b 1b
1c 1c
1d 1d
1e 1e
1f 1f
20 20
! 21 21
" 22 22
# 23 23
$ 24 24
% 25 25
& 26 26
' 27 27
( 28 28
) 29 29
* 2a 2a
+ 2b 2b
, 2c 2c
- 2d 2d
. 2e 2e
/ 2f 2f
0 30 30
1 31 31
2 32 32
3 33 33
4 34 34
5 35 35
6 36 36
7 37 37
8 38 38
9 39 39
: 3a 3a
; 3b 3b
< 3c 3c
= 3d 3d
> 3e 3e
? 3f 3f
@ 40 40
A 41 41
B 42 42
C 43 43
D 44 44
E 45 45
F 46 46
G 47 47
H 48 48
I 49 49
J 4a 4a
K 4b 4b
L 4c 4c
M 4d 4d
N 4e 4e
O 4f 4f
P 50 50
Q 51 51
R 52 52
S 53 53
T 54 54
U 55 55
V 56 56
W 57 57
X 58 58
Y 59 59
Z 5a 5a
[ 5b 5b
\ 5c 5c
] 5d 5d
^ 5e 5e
_ 5f 5f
` 60 60
a 61 61
b 62 62
c 63 63
d 64 64
e 65 65
f 66 66
g 67 67
h 68 68
i 69 69
j 6a 6a
k 6b 6b
l 6c 6c
m 6d 6d
n 6e 6e
o 6f 6f
p 70 70
q 71 71
r 72 72
s 73 73
t 74 74
u 75 75
v 76 76
w 77 77
x 78 78
y 79 79
z 7a 7a
{ 7b 7b
| 7c 7c
} 7d 7d
~ 7e 7e
7f 7f
� 80 80
� 81 81
� 82 82
� 83 83
� 84 84
� 85 85
� 86 86
� 87 87
� 88 88
� 89 89
� 8a 8a
� 8b 8b
� 8c 8c
� 8d 8d
� 8e 8e
� 8f 8f
� 90 90
� 91 91
� 92 92
� 93 93
� 94 94
� 95 95
� 96 96
� 97 97
� 98 98
� 99 99
� 9a 9a
� 9b 9b
� 9c 9c
� 9d 9d
� 9e 9e
� 9f 9f
� a0 a0
� a1 a1
� a2 a2
� a3 a3
� a4 a4
� a5 a5
� a6 a6
� a7 a7
� a8 a8
� a9 a9
� aa aa
� ab ab
� ac ac
� ad ad
� ae ae
� af af
� b0 b0
� b1 b1
� b2 b2
� b3 b3
� b4 b4
� b5 b5
� b6 b6
� b7 b7
� b8 b8
� b9 b9
� ba ba
� bb bb
� bc bc
� bd bd
� be be
� bf bf
� c0 c0
� c1 c1
� c2 c2
� c3 c3
� c4 c4
� c5 c5
� c6 c6
� c7 c7
� c8 c8
� c9 c9
� ca ca
� cb cb
� cc cc
� cd cd
� ce ce
� cf cf
� d0 d0
� d1 d1
� d2 d2
� d3 d3
� d4 d4
� d5 d5
� d6 d6
� d7 d7
� d8 d8
� d9 d9
� da da
� db db
� dc dc
� dd dd
� de de
� df df
� e0 e0
� e1 e1
� e2 e2
� e3 e3
� e4 e4
� e5 e5
� e6 e6
� e7 e7
� e8 e8
� e9 e9
� ea ea
� eb eb
� ec ec
� ed ed
� ee ee
� ef ef
� f0 f0
� f1 f1
� f2 f2
� f3 f3
� f4 f4
� f5 f5
� f6 f6
� f7 f7
� f8 f8
� f9 f9
� fa fa
� fb fb
� fc fc
� fd fd
� fe fe
� ff ff
100 100
101 101
102 102
103 103
104 104
105 105
106 106
107 107
108 108

RUN FINISHED; exit value 0; real time: 10ms; user: 0ms; system: 0ms


but if i put them like in two different loops it work
and i get ascii and int
then
ascii and hex
but i want them together not in a different loop
what i want is like this

ascii int hex

any help

Answer

The setting for the base in which to print integers is "sticky", so it remains set until you change it. To mix decimal and hex like this, you'll need to explicitly set it before each item you print:

#include <iostream>

using namespace std;
int main() {
    for (int i = 0; i < 265; i++) {
        cout << char(i) << "\t";
        cout << dec << i << "\t";
        cout << hex << i << "\n";
    }
}

Also note that i is already an int, so the cast to int was unnecessary.

Likewise I'd advise avoiding std::endl completely. "\n" suffices for the task at hand, and while you probably don't care about the difference in speed in this particular case, it is quite a bit faster as a rule (and in other cases, the speed difference really does matter).

You only need to include <iomanip> when you use a manipulator that takes an argument, like std::setw(5), not the ones that don't take arguments like std::hex (strange rule, I know).