【问题标题】:Sprintf of MAC address of available networks可用网络 MAC 地址的 Sprintf
【发布时间】:2013-12-11 10:02:51
【问题描述】:

我想 sprintf 在这个区域找到的一些网络的 Mac 地址,如下所示:

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

我的代码是:

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);
}

错误是:

C:\Users\h\Desktop\WiFi test\taskFlyport.c:22: 警告:传递 'sprintf' 的参数 1 中的指针目标在符号上不同

C:\Users\h\Desktop\WiFi test\taskFlyport.c:27: 警告:传递 '_dbgwrite' 的参数 1 中的指针目标在符号上不同

有没有人告诉我我的问题在哪里? 谢谢,问候

【问题讨论】:

    标签: c printf mac-address


    【解决方案1】:

    您也可以使用类似某些项目中所做的事情 wpa_supplicant:

    他们定义了一些宏来帮助打印 MAC 地址 (link):

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

    最后像这样使用它(link):

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

    输出是:

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

    这在整个项目中更容易使用。

    【讨论】:

      【解决方案2】:

      BYTE 是一个unsigned char,因此MAC 是一个unsigned char[]

      sprintf 想要char*

      将 MAC 的声明更改为:

      char MAC[64];
      

      这是一个演示:

      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);
      

      输出:

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

      【讨论】:

      • 谢谢回复。我根据相关结构定义写了字节,现在我没有任何警告但我的结果是一样的,就像:MAC:??5217356f,和MAC:?? 5217
      • @HOsseiNSA 这是因为您的循环以i = 1 开头,导致您跳过MAC 中的前两个字符。
      • @HOsseiNSA - 添加了代码。请注意,您使用的 printf 格式不适用于大数字(64 位)。在答案中查看我的演示代码。
      猜你喜欢
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2016-11-17
      相关资源
      最近更新 更多