Question

I want to sprintf Mac address of some found networks in this area like this:

      `WiFi connection settings:
      MAC: 00 1E C0 10 3B 19
       SSID:         css`

my code is :

char buf[32];
BYTE MAC[64];
int i;

for(i=1;i<15;i++)
{   
    MyScanResults = WFScanList(i);
    sprintf(buf,"%s", MyScanResults.ssid);
    sprintf(&MAC[i*2],"%02x", MyScanResults.bssid[i]);
    _dbgwrite("SSID:         ");
    _dbgwrite(buf);
    _dbgwrite("\n");
    _dbgwrite("MAC:         ");
    _dbgwrite(MAC);
}

and Errors are :

C:\Users\h\Desktop\WiFi test\taskFlyport.c:22: warning: pointer targets in passing argument 1 of 'sprintf' differ in signedness <

C:\Users\h\Desktop\WiFi test\taskFlyport.c:27: warning: pointer targets in passing argument 1 of '_dbgwrite' differ in signedness<

is there anyone to tell me where is my problem? thanks,regards

Was it helpful?

Solution 2

BYTE is an unsigned char and MAC is thus an unsigned char[].

sprintf wants char*.

Change MAC's declaration to:

char MAC[64];

Here's a demo:

char mac[64];
unsigned long long testMac = 0xAABBCCDDEEFF;

// without spaces
sprintf(mac, "MAC is: %llX", testMac);
printf("%s\n", mac);

// with spaces - not sure if order is correct!
unsigned char* pTestMac = (unsigned char*)&testMac;
sprintf(mac, "MAC is: %X %X %X %X %X %X",
    (unsigned)pTestMac[5],
    (unsigned)pTestMac[4],
    (unsigned)pTestMac[3],
    (unsigned)pTestMac[2],
    (unsigned)pTestMac[1],
    (unsigned)pTestMac[0]
    );
printf("%s\n", mac);

Output:

MAC is: AABBCCDDEEFF
MAC is: AA BB CC DD EE FF

OTHER TIPS

You can also use something like what is done in some projects as wpa_supplicant:

They define some macros to help printing MAC adresses (link):

#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"

And finally use it like that (link):

unsigned char mac[6] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
printf("My mac is " MACSTR "\n", MAC2STR(mac));

The output is:

My mac is 11:22:33:44:55:66

Which is much easier to use in a whole project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top