c語言中將整數轉換成字符串_在C語言中將ASCII字符串(char [])轉換為八進制字符串(char [])...

c語言中將整數轉換成字符串

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

給定一個ASCII字符串(char []),我們必須在C中將其轉換為八進制字符串(char [])。

Logic:

邏輯:

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

要將ASCII字符串轉換為八進制字符串,請執行以下步驟:

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

    從輸入字符串中提取字符,并使用%02o格式說明符將其轉換為八進制格式, %02o給出0填充的兩個字節的八進制值(例如int , char )。

  • Add these two bytes (characters) which is a octal 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: "111415151540161516151441"

C程序將ASCII char []轉換為八進制char [] (C program to convert ASCII char[] to octal char[])

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

在此示例中, ascii_str是包含“ Hello world!”的輸入字符串 ,我們將其轉換為八進制字符串。 在這里,我們創建了一個函數void string2OctalString(char * input,char * output) , 將ASCII字符串轉換為八進制字符串 ,最終的輸出字符串存儲在oct_str變量中。

#include <stdio.h>
#include <string.h>
//function to convert ascii char[] to octal-string (char[])
void string2OctalString(char* input, char* output)
{
int loop;
int i; 
i=0;
loop=0;
while(input[loop] != '\0')
{
sprintf((char*)(output+i),"%02o", 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 oct_str[(len*2)+1];
//converting ascii string to octal string
string2OctalString(ascii_str, oct_str);
printf("ascii_str: %s\n", ascii_str);
printf("oct_str: %s\n", oct_str);
return 0;
}

Output

輸出量

ascii_str: Hello world!
oct_str: 111415151540161516151441

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-octal-string-in-c.aspx

c語言中將整數轉換成字符串

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379772.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379772.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379772.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Javascript的IE和Firefox兼容性匯編收藏.txt

document.form.item 問題 現有問題&#xff1a;現有代碼中存在許多 document.formName.item("itemName") 這樣的語句&#xff0c;不能在 MF 下運行 解決方法&#xff1a;改用 document.formName.elements["elementName"] 集合類對象問題 現有問題&#xff…

FreeRTOS系統配置文件FreeRTOSConfig.h

實際使用FreeRTOS的時候&#xff0c;我們時常需要根據自己需求來配置FreeRTOS&#xff0c;而且不同架構的MCU在使用的時候配置也不同&#xff0c;FreeRTOS的系統配置文件FreeRTOSConfig.h可以完成FreeRTOS的裁剪和配置。FreeRTOSConfig.h分成兩個部分&#xff0c;一個是INCLUDE…

SQL更新多條數據

問題&#xff1a;有兩個不同的表&#xff0c;其中都有一個編號的字段&#xff0c;而且存儲的內容是相同的&#xff0c;需要將一張表中的另外一些字段依據編號去與另一個表中編號對應來更新到另一個表中。 方法&#xff1a;由于在sql中是不支持同時更新多條包含編號的數據的&…

簡單的登錄系統(java+JFrame+Mysql)

連接數據庫 package 注冊信息; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class conn_db{ Connection con; String url null; Statement stmt; public void connection() throws ClassNotFoundException{ //…

冬季止咳化痰的飲食偏方集錦

1、蘿卜蔥白風寒咳嗽 蘿卜1個,蔥白6根,生姜15克.用水三碗先將蘿卜煮熟,再放蔥白,姜,煮剩一碗湯.連渣一次服.宣肺解表,化痰止咳.治風寒咳嗽,痰多泡沫,伴畏寒,身倦酸痛等. 2、紅糖姜棗湯治傷風咳嗽 紅糖30克,鮮姜15克,紅棗30克. 以水三碗煎至過半.頓服,服后出微汗即愈. 驅風散寒.…

c語言中數組越界怎么辦_如果我們使用C語言數組中的越界索引怎么辦?

c語言中數組越界怎么辦Let’s understand first, what is index out of bounds? 首先讓我們了解一下 &#xff0c; 什么是索引超出范圍&#xff1f; Let suppose you have an array with 5 elements then the array indexing will be from 0 to 4 i.e. we can access element…

FreeRTOS任務基礎知識

任務特性 在RTOS中&#xff0c;一個實時應用可以作為一個獨立的任務&#xff0c;支持搶占&#xff0c;支持優先級&#xff0c;每個任務都有自己的堆棧&#xff0c;當任務切換時將上下文環境保存在堆棧中&#xff0c;再次調用任務時&#xff0c;取出上下文信息&#xff0c;繼續…

測試Rockey 4 Smart加密鎖的C語言代碼

測試Rockey 4 Smart加密鎖的C語言代碼 // win32Console_dog_test.cpp : Defines the entry point for the console application. /// // //測試Rockey 4 Smart加密鎖的C語言代碼 // /// #include "stdafx.h" #include <conio.h> #include "time.h" #…

C——任意一個偶數分解兩個素數

題目&#xff1a;一個偶數總能表示為兩個素數之和 以上實例運行輸出結果為&#xff1a; 請輸入一個偶數: 4 偶數4可以分解成1和3兩個素數的和 #include <stdio.h> #include <stdlib.h> int Isprimer(int n); int main() {int n,i;do{printf("請輸入一個偶數&…

c#委托調用另一窗口函數_在C#中使用委托調用成員函數

c#委托調用另一窗口函數Prerequisite: Delegates in C# 先決條件&#xff1a; C&#xff03;中的代表 We can also call a member function of a class using delegates. It is similar to static function calls, here we have to pass member function using an object on t…

Java版AVG游戲開發入門[0]——游戲模式轉換中的事件交互

Java版AVG游戲開發入門[0]——游戲模式轉換中的事件交互 示例程序下載地址&#xff1a;http://download.csdn.net/source/999273&#xff08;源碼在jar內&#xff09; AVG&#xff0c;即Adventure Game&#xff0c;可以直譯為[冒險游戲]。但是通常情況下我們說AVG是指[文字冒險…

FreeRTOS任務創建和刪除

任務創建和刪除的API函數 xTaskCreate()&#xff1a;使用動態方法創建一個任務xTaskCreateStatic()&#xff1a;使用靜態方法創建一個任務xTaskCreateRestricated()&#xff1a;創建一個使用MPU進行限制的任務&#xff0c;相關內存使用動態內存分配vTaskDelete()&#xff1a;刪…

Delphi 調試

調試&#xff1a;F9執行F8逐過程單步調試F7逐語句單步調試轉載于:https://www.cnblogs.com/JackShao/archive/2012/04/30/2476931.html

1.創建單項鏈表

# include <stdio.h> # include <malloc.h> # include <stdlib.h>typedef struct Node{int data;//數據域struct Node *pNext;//指針域}NODE, *PNODE; //NODE等價于struct Node //PNOD等價于struct Node * //函數聲明PNODE create_list(void); void traverse…

python 日本就業_日本的繪圖標志 Python中的圖像處理

python 日本就業Read basics of the drawing/image processing in python: Drawing flag of Thailand 閱讀python中繪圖/圖像處理的基礎知識&#xff1a; 泰國的繪圖標志 The national flag of Japan is a rectangular white banner bearing a crimson-red disc at its center…

[windows phone 7 ]查看已安裝程序GUID

首先介紹下wp7RootToolsSDK,這個功能相當強大&#xff0c;適合研究wp7高級功能。 它支持File&#xff0c;Register操作&#xff0c;比之前的COM調用要簡單&#xff0c;方便。 功能:查看已安裝程序的guid 開發心得: 用的是mozart,rom多&#xff0c;刷機吧&#xff0c;最麻煩的是…

FreeRTOS任務掛起和恢復

任務掛起&#xff1a;暫停某個任務的執行 任務恢復&#xff1a;讓暫停的任務繼續執行 通過任務掛起和恢復&#xff0c;可以達到讓任務停止一段時間后重新運行。 相關API函數&#xff1a; vTaskSuspend void vTaskSuspend( TaskHandle_t xTaskToSuspend );xTaskToSuspend &am…

向oracle存儲過程中傳參值出現亂碼

在頁面中加入<meta http-equiv"Content-Type" content"text ml;charsetUTF-8"/>就可以解決這一問題 適用情況&#xff1a; 1.中文 2.特殊符號 轉載于:https://www.cnblogs.com/GoalRyan/archive/2009/02/16/1391348.html

Scala程序將多行字符串轉換為數組

Scala | 多行字符串到數組 (Scala | Multiline strings to an array) Scala programming language is employed in working with data logs and their manipulation. Data logs are entered into the code as a single string which might contain multiple lines of code and …

SQL 異常處理 Begin try end try begin catch end catch--轉

SQL 異常處理 Begin try end try begin catch end catch 總結了一下錯誤捕捉方法:try catch ,error, raiserror 這是在數據庫轉換的時候用的的異常處理, Begin TryInsert into SDT.dbo.DYEmpLostTM(LogDate,ProdGroup,ShiftCode,EmployeeNo,MONo,OpNo,OTFlag,LostTypeID,OffStd…