c语言中将整数转换成字符串

Given an ASCII string (char[]) and we have to convert it into Hexadecimal string (char[]) in C.

给定一个ASCII字符串(char []),我们必须在C中将其转换为十六进制字符串(char [])。

Logic:

逻辑:

To convert an ASCII string to hex string, follow below-mentioned steps:

要将ASCII字符串转换为十六进制字符串,请执行以下步骤:

  • Extract characters from the input string and convert the character in hexadecimal format using %02X format specifier, %02X gives 0 padded two bytes hexadecimal value of any value (like int, char).

    从输入字符串中提取字符,并使用%02X格式说明符将其转换为十六进制格式, %02X为0填充两个字节的任意值的十六进制值(如int , char )。

  • Add these two bytes (characters) which is a hex value of an ASCII character to the output string.

    将这两个字节(字符)添加为输出字符串,这两个字节是ASCII字符的十六进制值。

  • After each iteration increase the input string's loop counter (loop) by 1 and output string's loop counter (i) by 2.

    每次迭代后,将输入字符串的循环计数器( loop )增大1,将输出字符串的循环计数器( i )增大2。

  • At the end of the loop, insert a NULL character to the output string.

    在循环末尾,在输出字符串中插入一个NULL字符。

Example:

例:

    Input: "Hello world!"Output: "48656C6C6F20776F726C6421"

C程序将ASCII char []转换为十六进制char [] (C program to convert ASCII char[] to hexadecimal char[])

In this example, ascii_str is an input string that contains "Hello world!", we are converting it to a hexadecimal string. Here, we created a function void string2hexString(char* input, char* output), to convert ASCII string to hex string, the final output string is storing in hex_str variable.

在此示例中, ascii_str是包含“ Hello world!”的输入字符串 ,我们将其转换为十六进制字符串。 在这里,我们创建了一个函数void string2hexString(char * input,char * output) , 将ASCII字符串转换为十六进制字符串 ,最终的输出字符串存储在hex_str变量中。

#include <stdio.h>
#include <string.h>
//function to convert ascii char[] to hex-string (char[])
void string2hexString(char* input, char* output)
{int loop;
int i;
i=0;
loop=0;
while(input[loop] != '\0')
{sprintf((char*)(output+i),"%02X", input[loop]);
loop+=1;
i+=2;
}
//insert NULL at the end of the output string
output[i++] = '\0';
}
int main(){char ascii_str[] = "Hello world!";
//declare output string with double size of input string
//because each character of input string will be converted
//in 2 bytes
int len = strlen(ascii_str);
char hex_str[(len*2)+1];
//converting ascii string to hex string
string2hexString(ascii_str, hex_str);
printf("ascii_str: %s\n", ascii_str);
printf("hex_str: %s\n", hex_str);
return 0;
}

Output

输出量

ascii_str: Hello world!
hex_str: 48656C6C6F20776F726C6421

Read more...

...

  • Octal literals in C language

    C语言的八进制文字

  • Working with octal numbers in C language

    使用C语言处理八进制数

  • Working with hexadecimal numbers in C language

    使用C语言处理十六进制数

翻译自: https://www.includehelp.com/c/convert-ascii-string-to-hexadecimal-string-in-c.aspx

c语言中将整数转换成字符串

c语言中将整数转换成字符串_在C语言中将ASCII字符串(char [])转换为十六进制字符串(char [])...相关推荐

  1. c语言中将整数转换成字符串_在C语言中将ASCII字符串(char [])转换为八进制字符串(char [])...

    c语言中将整数转换成字符串 Given an ASCII string (char[]) and we have to convert it into octal string (char[]) in ...

  2. angular 字符串转换成数字_一文看懂Python列表、元组和字符串操作

    好文推荐,转自CSDN,原作星辰StarDust,感觉写的比自己清晰-大江狗荐语. 序列 序列是具有索引和切片能力的集合. 列表.元组和字符串具有通过索引访问某个具体的值,或通过切片返回一段切片的能力 ...

  3. c语言.jpg图片转成数组_基于 C 语言开发的 GUI 框架

    一.介绍 AWTK全称Toolkit AnyWhere,是ZLG开发的开源GUI引擎,旨在为嵌入式系统.WEB.各种小程序.手机和PC打造的通用GUI引擎,为用户提供一个功能强大.高效可靠.简单易用. ...

  4. c语言将图像转换成字符画,25行Java代码将普通图片转换为字符画图片和文本的实现...

    本文主要介绍了25行Java代码将普通图片转换为字符画图片和文本的实现,分享给大家,具体如下: 原图 生成字符画文本(像素转换字符显示后,打开字符画显示相当于原图的好几倍大,不要用记事本打开,建议用n ...

  5. c语言实现补码转换成原码,(转)C语言之原码、反码和补码(示例代码)

    原码.反码和补码 1).数据在内存中存储的时候都是以二进制的形式存储的. int num = 10; 原码.反码.补码都是二进制.只不过是二进制的不同的表现形式. 数据是以补码的二进制存储的. 2). ...

  6. c语言 整数转换成二进制 模仿c++的 _itoa() 函数

    _itoa() 函数 头文件 :#include <stdlib.h> char *_itoa(int _value, char* _Dest, int _Radix); 功能:实现整数转 ...

  7. c语言 整数转二进制取位,C语言位运算--将整数转换成二进制串以及反转整数后N位...

    // c primer plus上的内容,位运算 #include char * itobs (int n,char * ps); void show_bstr(const char *); int ...

  8. 1.实现将整数转换成字符串

    将一个整数转换成字符串,拿到这个题目首先想到标准库里提供了哪些可用的函数. 如果是在window下编程,可以用系统库函数itoa实现该功能,itoa函数原型如下: char* itoa(int val ...

  9. c语言将字母转换成ascii码,c语言函数toascii()怎么把整数转换成合法的ASCII码字符...

    c语言函数toascii()怎么把整数转换成合法的ASCII码字符?函数需要引入的头文件:#include 定义toascii()函数:int toascii(int c); toascii()函数使 ...

最新文章

  1. 如何使用Python的进度条?
  2. Keras K.switch()用法
  3. 机器学习中Python常用库总结(numpy,scipy,matplotlib,pandas)
  4. 神经网络迭代次数的简并和不可约谱项
  5. Fiori应用的花瓣动画效果是怎么画出来的
  6. layui table工具栏点击时间_layui table表格上添加日期控件laydate
  7. CentOS Linux 系统命令之rmdir命令
  8. php mysql 单例模式_PHP单例模式_PHP单例模式数据库连接类
  9. 解决:Please specify a different SDK name--PyCharm报错
  10. 苹果iOS 16将改进通知 添加新的健康追踪功能
  11. cdn共振为什么要拍身份证_干货 | 拍婚纱照为什么要提前预约呢?
  12. ASP.NET 页面缓存 @ OutputCache
  13. 一个能让html标签变成文本的html标签lt;xmpgt;
  14. html compiler注册机,XXX计算器1.8注册分析和注册机代码
  15. java核心技术卷I 第4-5章
  16. 【干货】如何紧跟未来的设计趋势:15 个让你永远不过时的资源
  17. zoc7 下载和使用指南 连接远程主机
  18. 用水泥混凝土摊铺机进行作业时该做到的日常养护工作
  19. [刷机教程] [Root] S-OFF的同学来Root你的HTC Desire S
  20. python集合的概念_用Python中的集合Set讲解演示高一数学集合的概念

热门文章

  1. 循环队列的进队算法c语言,循环队列的定义,入队算法,出队算法,遍历算法,及其代码实现-Go语言中文社区...
  2. baseresponse响应类_内部类、响应类Response、序列化基类、反序列化、全局局部钩子...
  3. c++注释快捷键_Jupyter Notebook amp; Lab快捷键大全
  4. linq查询不包含某个值的记录_MySQL行(记录)的详细操作
  5. crv仪表上的i是什么指示灯_汽车打不着火是怎么回事,仪表盘汽车发动机故障灯亮是什么情况故障指示灯图解大全集...
  6. Linux fast open,Linux内核3.7 TCP Fast Open验证实例
  7. Linux基础(iptables与firewalld防火墙)
  8. python mockito arg_that_wqingxiao
  9. Spark-大规模数据处理计算引擎
  10. 日常问题——flume连接hive时报错Caused by: java.lang.NoSuchMethodError