char string to int

Hi,
I´m trying learn about bit, byte, char and string manipulation.
I do this code , but I can not understand my mistake.

sscanf(join, "%x", &temp)return 0 (zero) to temp. Why ?


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  // test_1
  
#include <stdio.h>
#include <stdlib.h>
 
typedef unsigned char byte;
	
int main()
{
	//unsigned int number = 0xADFE;
	int number = 0xADFE;
	int temp;
	char join[4] = {0,0,0,0}; 

	byte ab = (byte) (number >> 8);	
	byte cd = ((byte) number);		
	
	byte ab_hi =(0xF0 & ab)>>4;
	byte ab_lo = 0x0F & ab;
	
	byte cd_hi =(0xF0 & cd)>>4;
	byte cd_lo = 0x0F & cd;
	
	printf("\n\n Int Number...: %X", number);
	
	printf("\n\n u Char ab....: %X", ab);
	printf("\n\n u Char cd....: %X", cd);
	
	printf("\n\n u Char ab_hi.: %X", ab_hi);
	printf("\n\n u Char ab_lo.: %X", ab_lo);
	
	printf("\n\n u Char cd_hi.: %X", cd_hi);
	printf("\n\n u Char cd_lo.: %X", cd_lo);

//  Store unsigned char in string vector Char Join

	join[0] = ab_hi;
	join[1] = ab_lo; 
	join[2] = cd_hi;
	join[3] = cd_lo;

	printf("\n\n Char Join....: ");
	for(int i = 0; i < 4; i++)
	{
		printf("%X", join[i]);
	}

//  String vector Char Join to int
//	char join[4] = {'A','D','F','E'}; 

	sscanf(join, "%x", &temp);
	printf("\n\n Int Temp.....: %d",temp );

	printf("\n\n"); 

return 0;
}
Last edited on
closed account (48T7M4Gy)
Change line 12 to: int temp = 99; and you'll see why.
hi,
I do not undestand. This just will return 99 when print "tem" in line 52.
Why sscanf not work with my vector "join", returnig null ?
Topic archived. No new replies allowed.