首页 > 编程知识 正文

关于c语言tohexstring的信息

时间:2024-04-24 11:43:29 阅读:334411 作者:VMYP

本文目录一览:

怎样表示二进制

如果与JAVA相同:

主要成员方法:

public static String toHexString(long i);将i转换为16进制字符串

public static String toOctalString(long i);将i转换为8进制字符串

public static String toBinaryString(long i);将i转换为2进制字符串

toHexString

修改了一处

msg += "\u" +Integer.toHexString((int)chars[i]);后面加入了" "

msg += "\u" + Integer.toHexString((int) chars[i]) + " ";目的是为了方便字符串的划分(用\u划分字符串貌似不行)如果对这部有异议,可以选择其它划分字符串的方法.

添加了一个方法hexToString,用来将规定格式的句子还原为最初的字符串..按楼主题目的要求就是为了将十六进制数还原为"中华人民共和国"

public class strings {

static String msg = "";

public static void main(String[] args) {

msg = tohex();

System.err.println(msg);

String result = hextoString(msg);

System.err.println(result);

}

public static String tohex() {

String test = "中华人民共和国";

char[] chars = test.toCharArray();

for (int i = 0; i chars.length; i++) {

// 为了方便字符串划分,在每个十六进制数结尾加上了空格

msg += "\u" + Integer.toHexString((int) chars[i]) + " ";

}

return msg;

}

public static String hextoString(String msg) {

// 下面三句是利用正则表达式,用空格作为分隔符

// 获得每个"\u"+十六进制数子串,并保存与strs数组

Pattern pattern;

pattern = Pattern.compile(" ");

String[] strs = pattern.split(msg);

char[] foreString = new char[strs.length];

String hexNum;

for (int i = 0; i strs.length; i++) {

hexNum = strs[i].substring(2);// 获得"\u"后面的十六进制数

int value = Integer.parseInt(hexNum, 16);// 将其转化为十进制数

foreString[i] = (char) value;

}

return new String(foreString);

}

}

麻烦,不爱动手,上网查一下,就那么两个api,一用就ok了。easy的很。

#include winsock2.h

#include Iphlpapi.h

#include stdio.h

void byte2Hex(unsigned char bData,unsigned char hex[])

{

int high=bData/16,low =bData %16;

hex[0] = (high 10)?('0'+high):('A'+high-10);

hex[1] = (low 10)?('0'+low):('A'+low-10);

}

int getLocalMac(unsigned char *mac) //获取本机MAC地址

{

ULONG ulSize=0;

PIP_ADAPTER_INFO pInfo=NULL;

int temp=0;

temp = GetAdaptersInfo(pInfo,ulSize);//第一次调用,获取缓冲区大小

pInfo=(PIP_ADAPTER_INFO)malloc(ulSize);

temp = GetAdaptersInfo(pInfo,ulSize);

int iCount=0;

while(pInfo)//遍历每一张网卡

{

// pInfo-Address 是MAC地址

for(int i=0;i(int)pInfo-AddressLength;i++)

{

byte2Hex(pInfo-Address[i],mac[iCount]);

iCount+=2;

if(i(int)pInfo-AddressLength-1)

{

mac[iCount++] = ':';

}else

{

mac[iCount++] = '#';

}

}

pInfo = pInfo-Next;

}

if(iCount 0)

{

mac[--iCount]='';

return iCount;

}

else return -1;

}

int main(int argc, char* argv[])

{

unsigned char address[1024];

if(getLocalMac(address)0)

{

printf("mac-%sn",address);

}else

{

printf("invoke getMAC error!n");

}

return 0;

}

需要这两个:iphlpapi.lib , ws2_32.lib 静态库(VC添加到工程LINK里)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。