C:按字符发送字符串的字符为十六进制重新presentation [英] c: sending string character by character as hex representation
本文介绍了C:按字符发送字符串的字符为十六进制重新presentation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
char str[] = "05AD2101";
我有一个函数sendChar,我想在一对二的十六进制值发送此海峡字符如下:
I have a function sendChar and I want to send this str chars in a pair of two as hex values as follows:
sendChar(0x05);
sendChar(0xAD);
sendChar(0x21);
sendChar(0x01);
我该怎么办呢?
推荐答案
我没有测试过这一点,有可能是语法错误:
I did not tested this, there could be syntax errors:
int char2hex(char c){
switch(c){
case '0' : return 0x00;
case '1' : return 0x01;
case '2' : return 0x02;
// until 9
case 'A' : return 0x0a;
case 'B' : return 0x0b;
case 'C' : return 0x0c;
// until F
}
return -1; // unknown char
}
void send(char str[]){
if (!str)
return;
int i;
for(i = 0; i < strlen(str); ++i)
sendChar(char2hex(str[i]));
}
如果您需要更多帮助,请评论。
If you need more help, pls comment.
更新:
有可能有很多的改进,检查NULL,就把常量
各地,检查小写字母A,B,C,D,E,F等。
There could be many improvements, checking for NULL, put const
around, checking for lowercase A,B,C,D,E,F etc.
您甚至可以做这样的事情 - 但测试它之前使用它:
You could even do something like - but test it prior use it:
int char2hex(char c){
if (c < 'A')
return c - '0';
else
return c - 'A' + 10;
}
这篇关于C:按字符发送字符串的字符为十六进制重新presentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文