Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to store the mac address in a variable retrieved from the code in the for loop mentioned below

What I have tried:

int a;
	char MacAddr[6];
	ULONG size = sizeof(MacAddr);
	char jk;
	SendARP(inet_addr(argv[1]), inet_addr(ipv4.c_str()), MacAddr, &size);
	//std::cout << "Ipv4 is:  "+ipv4;
	std::cout << "\nThe MAC address is: ";
	for (unsigned char c : MacAddr)
		std::cout << std::setw(2) << std::setprecision(2) << std::setfill('0') << std::hex << (unsigned)c << " ";
Posted
Updated 3-Feb-20 20:09pm
Comments
phil.o 3-Feb-20 5:19am    
Unclear: storing anything in a variable is trivial (variable = value;). The MacAddr variable already holds the value.
Please use the green Improve question widget which appears on hovering your question, and clarify it.
Member 12899279 3-Feb-20 5:43am    
as you can see i am displaying mac address after converting it in hex and i want to use the same format in which its being displayed through for loop mechanism i want to store it in string variable so next time i can simply print string variable on the screen

Quote:
as you can see i am displaying mac address after converting it in hex and i want to use the same format in which its being displayed through for loop mechanism i want to store it in string variable so next time i can simply print string variable on the screen
Then use a std::ostringsteam, instead of std::cout.
Have a look at ostringstream::ostringstream - C++ Reference[^] (see the code sample).
 
Share this answer
 
Comments
Maciej Los 3-Feb-20 11:16am    
5ed!
CPallini 3-Feb-20 15:40pm    
Thank you, Maciej!
got it working with this
std::stringstream ss;
for (unsigned char c : MacAddr)
		ss<<std::setw(2) << std::setprecision(2) << std::setfill('0') << std::hex << (unsigned)c << " ";
	std::string sMAC = ss.str();
	cout << "\nMac address is:  " + sMAC;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900