convert a series of 32-bit integer values into a sequence of printable 8-bit character values.

Sep 10, 2013 at 6:36am
I want to do this manually as i dont know how to do it !!

This is a sample input >.

757935403
Sep 10, 2013 at 6:46am
there a many ways to do this and the simplest one is shifting:
1
2
3
4
5
6
7
8
9
10
11
12
	unsigned test = 757935403;
	char signs[4];

	signs[3] = test&0xff;
	signs[2] = (test>>8)&0xff;
	signs[1] = (test>>16)&0xff;
	signs[0] = (test>>24)&0xff;

	cout << signs[3] << endl;
	cout << signs[2] << endl;
	cout << signs[1] << endl;
	cout << signs[0] << endl;


or by casting (i like this way more:)):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct asd
{
	char val[4];
};

	unsigned test = 757935403;

	asd * lala;
	 
	lala = (asd*) &test;

	cout << lala->val[3] << endl;
	cout << lala->val[2] << endl;
	cout << lala->val[1] << endl;
	cout << lala->val[0] << endl;

Sep 10, 2013 at 6:49am
Yeah but can you do manually like on a paper !! because i dont have no idea what the hell is this thing is !!!
i got ascii table .. but how do i find out !!
Sep 10, 2013 at 6:55am
You need to convert this number representation from decimal (757935403) to hexadecimal (0x2D2D2D2B).

Then you will cleary see the bytes: 0x2D (45) 0x2D (45) 0x2D (45) 0x2B (43)
How to convert to hexadecimal look on this site, google or on youtube.
Last edited on Sep 10, 2013 at 6:55am
Sep 10, 2013 at 7:13am
yeah thats my problem i think

thanks
Sep 10, 2013 at 10:20am
We use hex because it's so easy to see what bits are set where. Decimal doesn't give us individual bits, and binary takes forever to read because you have to count all of the 1s and 0s.

0x0 = 0000  0x8 = 1000
0x1 = 0001  0x9 = 1001
0x2 = 0010  0xA = 1010
0x3 = 0011  0xB = 1011
0x4 = 0100  0xC = 1100
0x5 = 0101  0xD = 1101
0x6 = 0110  0xE = 1110
0x7 = 0111  0xF = 1111


It's best to memorize your hex numbers. Then when you have a 32 bit number 757935403, you can convert it on your programming calculator to 2D 2D 2D 2B and you can see what each bit is doing.

If you want to convert that to ASCII, look it up on your ASCII table. You'll see it turns into
---+
.

Another easy way to do this in code is to do this with the <iomanip> library:
std::cout << std::hex << test;
or to split it up, you can also use unions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>

union
{
    unsigned i;
    char c[4];
} test;

int main()
{
    test.i = 757935403;
    std::cout << std::hex << test.i << std::endl;
    std::cout << test.c[3] << test.c[2] << test.c[1] << test.c[0];
}
2d2d2d2b
---+
Last edited on Sep 10, 2013 at 10:28am
Topic archived. No new replies allowed.