一串字符串轉換為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.pswp.cn/news/541708.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541708.shtml
英文地址,請注明出處:http://en.pswp.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;當然更多的是自己給自己挖的…