C语言 如何将 MAC 地址(在数组中)转换为 C 中的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16550589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you convert a MAC address (in an array) to string in C?
提问by user2131316
How do you convert a MAC address within an intarray to string in C? For example, I am using the following array to store a MAC address:
如何将int数组中的 MAC 地址转换为C 中的字符串?例如,我使用以下数组来存储 MAC 地址:
int array[6] = {0x00, 0x0d, 0x3f, 0xcd, 0x02, 0x5f};
How do I convert this to a string, like "00:0d:3f:cd:02:5f"?
如何将其转换为字符串,例如"00:0d:3f:cd:02:5f"?
回答by user2233706
You could do this:
你可以这样做:
char macStr[18];
int array[6] = {0x00, 0x0d, 0x3f, 0xcd, 0x02, 0x5f};
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
array[0], array[1], array[2], array[3], array[4], array[5]);
回答by Gisway
unsigned char array[6] = {0x00, 0x0d, 0x3f, 0xcd, 0x02, 0x5f};//or BYTE
char str[19];
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x",array[0],
array[1], array[2], array[3], array[4],array[5]);
回答by Selso Liberado
whithout using snprintf, but that's just for fun....
没有使用 snprintf,但这只是为了好玩....
#define MAC_LEN 6
static const char _mac[MAC_LEN] = { 0xBC, 0xDD, 0xC2, 0xF0, 0x2E, 0x06 };
int main(void){
char mac[MAC_LEN*2 + 1] = {0}; // added null char
int j = 0;
for( int i = 0; i < sizeof(_mac) ; i++ )
{
j = i * 2;
mac[j] = (((_mac[i] & 0xF0)>>4)&0xF) ;
mac[j] += (mac[j] <= 9) ? '0' : ('A' - 10);
j++;
mac[j] = (_mac[i] & 0x0F);
mac[j] += (mac[j] <= 9) ? '0' : ('A' -10);
}
printf("Hello World!, my mac address : %s\n", mac);
fflush(stdout);
return 0;
}
回答by Sandeep K V
If you are working on low level Kernel or driver stuff where the stdlib features cannot be used then it can be converted as follows-
如果您正在处理无法使用 stdlib 功能的低级内核或驱动程序内容,则可以按如下方式进行转换 -
#include <stdio.h>
struct ether_addr{
int ether_addr_octet[6];
};
const char* ntoa(struct ether_addr ea)
{
static char mstr[13];
int j=11;
for (int i=5;i>=0;i--)
{
int val = ea.ether_addr_octet[i];
while (val)
{
int k = val%16;
if (k==10)
mstr[j--] = 'A';
if (k==11)
mstr[j--] = 'B';
if (k==12)
mstr[j--] = 'C';
if (k==13)
mstr[j--] = 'D';
if (k==14)
mstr[j--] = 'E';
if (k==15)
mstr[j--] = 'F';
if (k<10)
mstr[j--] = k + '0';
val = val/16;
}
}
mstr[12] = '#include <stdio.h>
unsigned char mac[6] = {0x00, 0x0d, 0x3f, 0xcd, 0x02, 0x5f};
char* MACADDR_toString(unsigned char* addr)
{
static char str[18];
if(addr == NULL) return "";
snprintf(str, sizeof(str), "%02x:%02x:%02x:%02x:%02x:%02x",
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
return str;
}
char* MACADDR_toString_r(unsigned char* addr, char* str, size_t size)
{
if(addr == NULL || str == NULL || size < 18) return NULL;
snprintf(str, size, "%02x:%02x:%02x:%02x:%02x:%02x",
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
return str;
}
int main(int argc, char* argv[])
{
char str[18];
printf("%s\n", MACADDR_toString(mac));
printf("%s\n", MACADDR_toString_r(mac, str, sizeof(str)));
return 0;
}
';
char *parr = mstr;
printf("%6s\n",mstr);
return mstr;
}
int main(void) {
// your code goes here
struct ether_addr ue;
ue.ether_addr_octet[0] = 255;
ue.ether_addr_octet[1] = 255;
ue.ether_addr_octet[2] = 255;
ue.ether_addr_octet[3] = 255;
ue.ether_addr_octet[4] = 255;
ue.ether_addr_octet[5] = 255;
char *s = ntoa(ue);
printf("%s",s);
return 0;
}
回答by mpontillo
First, you probably want to adjust the type you use to store the MAC address. I think unsigned charis better in this case. Next, I recommend you create a function to write the MAC addresses, so you aren't copying and pasting the same printf()statement everywhere in your code and adjusting the array you're indexing. (also, it allows the compiler to check the type you're using against the function parameter to ensure it is correct.)
首先,您可能想要调整用于存储 MAC 地址的类型。我认为unsigned char在这种情况下更好。接下来,我建议您创建一个函数来写入 MAC 地址,这样您就不会printf()在代码中的任何地方复制和粘贴相同的语句并调整您正在编制索引的数组。(此外,它允许编译器根据函数参数检查您使用的类型以确保它是正确的。)
Here's both a non-reentrant solution similar to inet_ntoa(), and a re-entrant solution similar to inet_ntoa_r():
这是类似于 的不可重入解决方案和类似于 的可重入解决inet_ntoa()方案inet_ntoa_r():
#include <stdio.h>
int array[6] = {0x00, 0x0d, 0x3f, 0xcd, 0x02, 0x5f};
void macaddress_str(int a[], char *buf)
{
int i;
for (i = 0; i < 5; i++, buf += 3)
sprintf(buf, "%02X:", a[i]);
sprintf(buf, "%02X", a[i]);
}
int main()
{
char buf[100];
macaddress_str(array, buf);
printf("%s\n", buf);
return 0;
}
回答by scottt
Call sprintf()in a loop ^_^:
在循环中调用sprintf()^_^:
##代码##
