一串字符串转换为ascii_将ASCII字符串(char [])转换为C中的BYTE数组

一串字符串转换为ascii

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

给定一个ASCII字符串(char []),我们必须将其转换为C语言中的BYTE数组(BYTE [])。

Logic:

逻辑:

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

要将ASCII字符串转换为BYTE数组,请执行以下步骤:

  • Extract characters from the input string and get the character's value in integer/number format using %d format specifier, %d gives integer (number) i.e. BYTE value of any character.

    从输入字符串中提取字符,并使用%d格式说明符以整数/数字格式获取字符的值, %d给出整数(数字),即任何字符的BYTE值。

  • Add these bytes (number) which is an integer value of an ASCII character to the output array.

    将这些字节(数字)(它是ASCII字符的整数值)添加到输出数组中。

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

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

Example:

例:

    Input: "Hello world!"
Output:
72
101
108
108
111
32 
119
111
114
108
100
33

C程序将ASCII char []转换为BYTE数组 (C program to convert ASCII char[] to BYTE array)

In this example, ascii_str is an input string that contains "Hello world!", we are converting it to a BYTE array. Here, we created a function void string2ByteArray(char* input, BYTE* output), to convert ASCII string to BYTE array, the final output (array of integers) is storing in arr variable, which is passed as a reference in the function.

在此示例中, ascii_str是包含“ Hello world!”的输入字符串 ,我们正在将其转换为BYTE数组。 在这里,我们创建了一个函数void string2ByteArray(char * input,BYTE * output) , 将ASCII字符串转换为BYTE array ,最终输出(整数数组)存储在arr变量中,该变量作为函数中的引用传递。

Note: Here, we created a typedef BYTE for unsigned char data type and as we know an unsigned char can store value from 0 to 255.

注意:在这里,我们为无符号字符数据类型创建了一个typedef BYTE ,众所周知, 无符号字符可以存储0到255之间的值。

Read more: typedef in C, unsigned char in C

C语言中的typedef,C语言中的unsigned char

#include <stdio.h>
#include <string.h>
typedef unsigned char BYTE;
//function to convert string to byte array
void string2ByteArray(char* input, BYTE* output)
{
int loop;
int i;
loop = 0;
i = 0;
while(input[loop] != '\0')
{
output[i++] = input[loop++];
}
}
int main(){
char ascii_str[] = "Hello world!";
int len = strlen(ascii_str);
BYTE arr[len];
int i;
//converting string to BYTE[]
string2ByteArray(ascii_str, arr);
//printing
printf("ascii_str: %s\n", ascii_str);
printf("byte array is...\n");
for(i=0; i<len; i++)
{
printf("%c - %d\n", ascii_str[i], arr[i]);
}
printf("\n");
return 0;
}

Output

输出量

ascii_str: Hello world!
byte array is...
H - 72
e - 101
l - 108
l - 108
o - 111
- 32 
w - 119
o - 111
r - 114
l - 108
d - 100
! - 33

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-byte-array-in-c.aspx

一串字符串转换为ascii

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541708.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

debugging Auto Layout:Logical Errors

Logical Errors逻辑错误 Logical errors are simply bugs. Somewhere, you have an assumption that is faulty. Perhaps it’s an assumption about how Auto Layout calculates the views’ frames. Perhaps it’s an assumption about the set of constraints that you’ve …

linux反序列化漏洞,思科多个产品Java反序列化漏洞(CVE-2015-6420)

思科多个产品Java反序列化漏洞(CVE-2015-6420)发布日期&#xff1a;2015-12-15更新日期&#xff1a;2015-12-17受影响系统&#xff1a;Cisco Unified ComputingCisco Voice and Unified Communications DevicesCisco Wireless描述&#xff1a;CVE(CAN) ID: CVE-2015-6420思科是…

密码学替代技术_替代技术及其类型| 密码学

密码学替代技术As we already discussed what are the Substitution techniques and one of its type Ceasar Cipher? So we are not discussing it here for that please refer to Cryptography: CeasarCipher here: Cryptography: Caesar Cipher and its Python Implementat…

Flask+uwsgi+Nginx环境搭建

2019独角兽企业重金招聘Python工程师标准>>> 开源软件准备 需要的软件列表&#xff1a; setuptools-33.1.1.zip Python-2.7.13.tgz pip-9.0.1.tar.gz nginx-1.10.3.tar.gz 软件统一上传到/usr/local/src/下&#xff0c;python是使用自己编译的。Python安装 先安装以…

穿越迷宫c语言程序设计教程课后答案,实验二 迷宫实验.doc

#include #define ROW 11#define COLUMN 15typedef struct{ /*栈中的数据元素的类型定义*/int row; /*行下标*/int col; /*列下标*/int direction; /*下一步移动方向*/} DATA;Typedif struct node{ /* 栈类定义*/DATA data;Struct node *next;}LinkStack;Typedef struct{/*移动…

ofb模式_密码学中的输出反馈模式(OFB)

ofb模式This is an output feedback (OFB) mode is similar in structure to that of CFB in Cryptography. It is the output of the encryption function that is fed back to the shift register in OFB in the cryptography, whereas in CFB in the mode of blocks, the ci…

JavaScript0-闭包

1.闭包的概念&#xff1a;在JavaScript中局部作用域总是能够访问到全局作用域&#xff0c;即内部函数总是能够访问到外部函数的参数和变量&#xff0c;即使内部函数调用完毕。也就是指有权访问到函数作用域里的变量。 function fn1() {var x 0;return function() {cosole.log(…

win8编程c语言,Win8系统怎么运行C语言 win8系统运行C语言的方法

C语言是一门通用计算机编程语言&#xff0c;是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言&#xff0c;但是许多win8系统用户并不知道要怎么运行C语言&#xff0c;针对这个情况&#xff0c;小编就给大家分享一…

stack示例_C.示例中的Stack.CopyTo()方法

stack示例C&#xff03;Stack.CopyTo()方法 (C# Stack.CopyTo() method) Stack.CopyTo() method is used to copy the stack elements/objects to an existing array from the given index. Stack.CopyTo()方法用于将堆栈元素/对象从给定索引复制到现有数组。 Syntax: 句法&am…

Linux sudoers文件的写法

2019独角兽企业重金招聘Python工程师标准>>> 文件的组成 sudoers文件由三部分组成&#xff1a; sudoers的默认配置&#xff0c;主要设置sudo的一些缺省值&#xff08;本文不会对这些默认配置进行介绍&#xff0c;若有兴趣可以自己man 5 sudoers然后搜defaults)alias…

if是什么c语言,这个C语言是什么(if(1))?

我在openssl源代码中注意到一个奇怪的成语,here并重复如下&#xff1a;if ((in NULL) && (passwds NULL)) {if (1) { (* #ifndef OPENSSL_NO_UI/* build a null-terminated list */static char *passwds_static[2] { NULL, NULL };passwds passwds_static;if (in …

c#queue_带有C#示例的Queue.CopyTo()方法

c#queueC&#xff03;Queue.CopyTo()方法 (C# Queue.CopyTo() method) Queue.CopyTo() method is used to copy the Queue elements/objects to an existing array from specified index. Queue.CopyTo()方法用于将Queue元素/对象从指定的索引复制到现有数组。 Syntax: 句法&a…

指针在c语言中的运用,怎么理解C语言中的指针,如何运用?

恰好我之前写了一系列介绍 C 语言的文章&#xff0c;介绍了什么是指针&#xff0c;以及为什么要使用指针&#xff0c;下面摘录一部分&#xff0c;感兴趣的话&#xff0c;可以点我了解更多。什么是 C语言指针&#xff1f;不同的数据类型的主要区别在于占用的存储空间不同。我们知…

设计模式(一)单例模式的七种写法

1. 饿汉模式 public class Singleton { private static Singleton instance new Singleton(); private Singleton (){}public static Singleton getInstance() { return instance; } } View Code这种方式在类加载时就完成了初始化&#xff0c;所以类加载较慢&#xff0c;…

scala 字符串转换数组_如何在Scala中将字节数组转换为字符串?

scala 字符串转换数组Byte Array in Scala is an array of elements of a byte type. String in Scala is a collection of the character data type. Scala中的字节数组是字节类型的元素的数组。 Scala中的String是字符数据类型的集合。 将字节数组转换为字符串 (Convert byt…

智能关机软件 c语言,智能关机软件

智能关机软件是一款免费共享关机软件。智能关机软件不但具有定时关机、自动关机的功能&#xff0c;而且还可以进行定时提醒信息、打开文件、打开网页、重启计算机、注销用户、锁定计算机、计算机休眠、计算机待机、关闭显示器&#xff0c;并且可以进行多任务计划&#xff0c;可…

wget: command not found

-bash: wget: command not found的两种解决方法 今天给服务器安装新LNMP环境时&#xff0c;wget 时提示 -bash:wget command not found,很明显没有安装wget软件包。一般linux最小化安装时&#xff0c;wget不会默认被安装。可以通过以下两种方法来安装&#xff1a;1、rpm 安装rp…

数据库数据规范化看不懂_数据库管理系统中的规范化

数据库数据规范化看不懂DBMS中的规范化 (Normalization in DBMS) Every table must have a single idea. The method by which we divide tables approximately is called normalization and the rest used for normalization is a functional dependency. For the normalizati…

c 语言开发一个四则运算器,C++实现四则运算器(无括号)

本文实例为大家分享了C实现无括号的四则运算器的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下完成度更高的带括号版本可以看C实现四则运算器(带括号)对于无括号的计算器&#xff0c;实现起来比较容易&#xff0c;下面让我们一步步实现。举例首先明确需要实现怎样的…

iOS开发之解决系统数字键盘无文字时delete键无法监听的技巧

最近在做用户登录获取验证码时添加图形验证码功能&#xff0c;就是只有正确输入图形验证码才能收到后台发送的短信验证码。效果如下&#xff1a; 看起来虽然是个小功能&#xff0c;但是实际操作起来&#xff0c;会发现苹果给我们留下的坑&#xff0c;当然更多的是自己给自己挖的…