一串字符串转换为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:7210110810811132 11911111410810033C程序將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);//printingprintf("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 ! - 33Read 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
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的一串字符串转换为ascii_将ASCII字符串(char [])转换为C中的BYTE数组的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: c语言100位整数变量声明_C ++程序
 - 下一篇: Python | Pyplot标签