我有一個十六進制字符串,並希望它在C中轉換爲ascii字符串。我怎麼能做到這一點?十六進制ASCII字符串轉換
10
A
回答
13
你需要採取2(十六進制)字符在同一時間......然後計算int值 ,之後使焦炭轉化喜歡...
char d = (char)intValue;
每一個做到這一點
#include <stdio.h>
#include <string.h>
int hex_to_int(char c){
int first = c/16 - 3;
int second = c % 16;
int result = first*10 + second;
if(result > 9) result--;
return result;
}
int hex_to_ascii(char c, char d){
int high = hex_to_int(c) * 16;
int low = hex_to_int(d);
return high+low;
}
int main(){
const char* st = "48656C6C6F3B";
int length = strlen(st);
int i;
char buf = 0;
for(i = 0; i < length; i++){
if(i % 2 != 0){
printf("%c", hex_to_ascii(buf, st[i]));
}else{
buf = st[i];
}
}
}
2
幾個大字就像alphabe:在十六進制字符串
這個工程如果字符串字符只有0-9A-F 2chars ts i-o無法轉換爲相應的ASCII字符。 like in string'6631653064316f30723161'對應於fedora。但它給出了fedra
只是稍微修改hex_to_int()函數,它將適用於所有字符。 修改功能是
int hex_to_int(char c)
{
if (c >= 97)
c = c - 32;
int first = c/16 - 3;
int second = c % 16;
int result = first * 10 + second;
if (result > 9) result--;
return result;
}
現在嘗試它將適用於所有字符。
0
strtol()
是你的朋友在這裏。第三個參數是您正在轉換的數字庫。
例子:
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main(int argc, char **argv)
{
long int num = 0;
long int num2 =0;
char * str. = "f00d";
char * str2 = "0xf00d";
num = strtol(str, 0, 16); //converts hexadecimal string to long.
num2 = strtol(str2, 0, 0); //conversion depends on the string passed in, 0x... Is hex, 0... Is octal and everything else is decimal.
printf("%ld\n", num);
printf("%ld\n", num);
}
相關問題
- 1. 將ASCII字符轉換爲十六進制轉義字符串
- 2. 十六進制字符串到ASCII轉換和ASCII到十六進制轉換問題
- 3. 將字符串轉換爲十六進制到十六進制
- 4. 轉換十六進制字符串十六進制值
- 5. 字符串爲十六進制和十六進制轉換爲字符串
- 6. JAVA轉換十六進制字符串
- 7. 將ASCII字符串轉換爲十進制和十六進制表示形式
- 8. 在Java中將十六進制字符串轉換爲ASCII碼
- 9. c#十六進制字符串到ASCII轉換結果'?'
- 10. 擴展ASCII字符串轉換爲十六進制格式
- 11. 轉換十六進制字符串爲ASCII格式
- 12. 從十六進制字符串轉換爲十六進制字符數組
- 13. 如何將十六進制字符串轉換爲Lua中的ascii字符串
- 14. 轉換十六進制字符串爲ASCII字符串顯示爲00
- 15. 如何將十六進制字符串轉換爲十進制?
- 16. 轉換十六進制,十進制,八進制和ASCII?
- 17. C#字符串爲十六進制,十六進制到字節轉換
- 18. 十進制數字符串轉換爲十六進制的字符串
- 19. 將十六進制值轉換爲ascii
- 20. ASCII /十六進制轉換在bash
- 21. 將十六進制轉換爲ascii
- 22. 轉換ASCII值以十六進制值
- 23. c#將十六進制轉換爲ASCII
- 24. 十六進制到ASCII轉換問題
- 25. 將ASCII十六進制代碼轉換爲「混合」字符串中的字符
- 26. 字符串轉換爲從ANSI /十六進制字符串
- 27. 將十六進制字符串轉換爲字符串
- 28. Swift3將字符串值轉換爲十六進制字符串
- 29. 轉換十六進制字符串轉換爲十進制在C++
- 30. C# - 轉換十六進制值的字符串爲十六進制
是不是一個十六進制字符串(如:' 「F00BA4」')ASCII字符串的特殊情況?或者正在使用EBCDIC? :) – pmg 2011-03-23 10:21:34