Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Formatting Strings in C Language?

Status
Not open for further replies.

xpress_embedo

Advanced Member level 4
Advanced Member level 4
Joined
Jul 5, 2011
Messages
1,154
Helped
161
Reputation
396
Reaction score
189
Trophy points
1,353
Location
India
embeddedlaboratory.blogspot.in
Activity points
10,591
Hello!! Everyone,
I have to convert Ip address array written in Decimal form to String form, i have written this code for it.


Code C - [expand]
1
2
char IP[4] = {192,168,1,17};    
char IP_Address[16];



// First Function


Code C - [expand]
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
void ConvertIpToString(char *IP,char *IP_Address)
{
  *(IP_Address)    = ((*IP)/100)|0x30;
  *(IP_Address+1)  = (((*IP)/10)%10)|0x30;
  *(IP_Address+2)  = ((*IP)%10)|0x30;
  
  *(IP_Address+3)  = '.';
  
  *(IP_Address+4)  = ((*(IP+1))/100)|0x30;
  *(IP_Address+5)  = (((*(IP+1))/10)%10)|0x30;
  *(IP_Address+6)  = ((*(IP+1))%10)|0x30;
  
  *(IP_Address+7)  = '.';
  
  *(IP_Address+8)  = ((*(IP+2))/100)|0x30;
  *(IP_Address+9)  = (((*(IP+2))/10)%10)|0x30;
  *(IP_Address+10) = ((*(IP+2))%10)|0x30;
  
  *(IP_Address+11) = '.';
  
  *(IP_Address+12) = ((*(IP+3))/100)|0x30;
  *(IP_Address+13)  = (((*(IP+3))/10)%10)|0x30;
  *(IP_Address+14)  = ((*(IP+3))%10)|0x30;
  
  *(IP_Address+15)  = 0;  // Null Character
}



This functions works properly, but i need some small code for that, which is as follow:

// Second Function


Code C - [expand]
1
2
3
4
void ConvertIpToString(char *IP,char *IP_Address)
{
snprintf(IP_Address,16,"%d.%d.%d.%d",*IP,*(IP+1),*(IP+2),*(IP+3));
}



Both functions works properly, just with a difference as below:
First Creates array as "192.168.001.017"
while second creates array "192.168.1.17"

Is it possible with snprintf to create an array of string similar to the one obtained by first function.

- - - Updated - - -

*********************************************************************

Hello!! Problem Solved.

By Using This Line.

Code:
snprintf(IP_Address,16,"%.3d.%.3d.%.3d.%.3d",*IP,*(IP+1),*(IP+2),*(IP+3));
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top