Printing Unsigned Char

Dec 21, 2011 at 10:12pm
Hi I am currently adding together two unsigned char however there seems to be 011 binary padding in result when printed. This my first time working with unsigned char, so i am not sure if this is normal.

Result of below: 98 100 102 104 106 108 110 112 114

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void main (int argc, char* argv[]){

	const unsigned char uc [] = "123456789";

	MParith mp;	
		
	for (int i = 0; i < 9; i++){
		cout << uc[i] << endl;
	}

	NRvector <unsigned char> w;
	NRvector <unsigned char> u;
	NRvector <unsigned char> v;
	u = NRvector<unsigned char>(9, uc);
	v = NRvector<unsigned char>(9, uc);

	mp.mpadd(w, u, v);
	
	cout << w.size() << endl;
		
	for (int i = 0; i < w.size(); i++){
		printf("%u", w[i]);
		cout << endl;
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
struct MParith {

	void mpadd(NRvector<unsigned char> &w, NRvector<unsigned char> &u, NRvector<unsigned char> &v) {
		w = NRvector<unsigned char>(v.size()+1);
		Int j, n=u.size(), m=v.size(), p=w.size();
		Int n_min=MIN(n,m),p_min=MIN(n_min,p-1);
		Uint ireg=0;
		for (j=p_min-1;j>=0;j--) {
			ireg=u[j]+v[j]+hibyte(ireg);
			printf("%X\t", ireg);
			printf("%u\t", ireg);
			printf("%d\n", ireg);
			w[j+1]=lobyte(ireg);
		}
		w[0]=hibyte(ireg);
		if (p > p_min+1)
			for (j=p_min+1;j<p;j++) w[j]=0;
	}

	void mpsub(Int &is, NRvector<unsigned char> &w, NRvector<unsigned char> &u, NRvector<unsigned char> &v) {
		Int j,n=u.size(),m=v.size(),p=w.size();
		Int n_min=MIN(n,m),p_min=MIN(n_min,p-1);
		Uint ireg=256;
		for (j=p_min-1;j>=0;j--) {
			ireg=255+u[j]-v[j]+hibyte(ireg);
			w[j]=lobyte(ireg);
		}
		is=hibyte(ireg)-1;
		if (p > p_min)
			for (j=p_min;j<p;j++) w[j]=0;
	}
Last edited on Dec 21, 2011 at 10:13pm
Dec 21, 2011 at 10:46pm
You are initialising with a text string "123456789"

This will result in ASCII codes for '1', '2' ...

which do indeed have 0011 as the upper nibble

see here

http://www.asciitable.com/

Dec 22, 2011 at 1:59am
How then should I initialize a unsigned char?
Dec 22, 2011 at 11:19am
I now get a bat ptr when using the below, why is that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void main (int argc, char* argv[]){
	string s = "123456789";
	const unsigned char *uc = new unsigned char[8];
	
	MParith mp;	
	uc = (unsigned char*)atoi(s.c_str());// "234567890";	
	//cout << uc;
	printf("%u", uc);
	NRvector <unsigned char> w;
	NRvector <unsigned char> u;
	NRvector <unsigned char> v;
	u = NRvector<unsigned char>(10, uc);
	v = NRvector<unsigned char>(9, uc);

	mp.mpadd(w, u, v);
	
	cout << w.size() << endl;
		
	for (int i = 0; i < w.size(); i++){
		printf("%u", w[i]);
		cout << endl;
	}
}
Dec 22, 2011 at 11:24am
1
2
3
	const unsigned char *uc = new unsigned char[8];
	...	
	uc = (unsigned char*)atoi(s.c_str());// "234567890"; 


Can't help with your bad pointer, but this is a memory leak. You make uc point at that nice array of 8 char you make, and then you make it point at the inside of the string S. You have lost forever that nice array of 8 char (that you never even used).
Dec 22, 2011 at 1:11pm
I would really like to know how to convert string to unsigned char without string literals. Is this the way to do it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void main (int argc, char* argv[]){
	string s = "123456789";
	const unsigned char cd[] = { 0x61, 0x62, 0x63, 0x0 };
	MParith mp;	
	const unsigned char *uc= (unsigned char*)atoi(s.c_str());// "234567890";	
	printf("%u", uc);
	NRvector <unsigned char> w;
	NRvector <unsigned char> u;
	NRvector <unsigned char> v;
	u = NRvector<unsigned char>(9, uc);
	v = NRvector<unsigned char>(9, uc);
	mp.mpadd(w, u, v);
	
	cout << w.size() << endl;
		
	for (int i = 0; i < w.size(); i++){
		printf("%u", w[i]);
		cout << endl;
	}
}
Dec 22, 2011 at 2:00pm
You can store values like this:
const unsigned char cd[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
And don't mix printf with cout, you can do the for loop like this:
1
2
for (int i = 0; i < w.size(); i++)
		cout << w[i] << endl;
Topic archived. No new replies allowed.