日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/1 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言中将整数转换成字符串_在C语言中将ASCII字符串(char [])转换为八进制字符串(char [])... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

c語言中將整數(shù)轉(zhuǎn)換成字符串

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

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

Logic:

邏輯:

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

要將ASCII字符串轉(zhuǎn)換為八進制字符串,請執(zhí)行以下步驟:

  • 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格式說明符將其轉(zhuǎn)換為八進制格式, %02o給出0填充的兩個字節(jié)的八進制值(例如int , char )。

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

    將這兩個字節(jié)(字符)添加為輸出字符串,這兩個字節(jié)是ASCII字符的八進制值。

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

    每次迭代后,將輸入字符串的循環(huán)計數(shù)器( loop )增大1,將輸出字符串的循環(huán)計數(shù)器( i )增大2。

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

    在循環(huán)末尾,在輸出字符串中插入一個NULL字符。

Example:

例:

Input: "Hello world!"Output: "111415151540161516151441"

C程序?qū)SCII char []轉(zhuǎn)換為八進制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!”的輸入字符串。 ,我們將其轉(zhuǎn)換為八進制字符串。 在這里,我們創(chuàng)建了一個函數(shù)void string2OctalString(char * input,char * output) , 將ASCII字符串轉(zhuǎn)換為八進制字符串 ,最終的輸出字符串存儲在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 stringoutput[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 bytesint len = strlen(ascii_str);char oct_str[(len*2)+1];//converting ascii string to octal stringstring2OctalString(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語言處理八進制數(shù)

  • Working with hexadecimal numbers in C language

    使用C語言處理十六進制數(shù)

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

c語言中將整數(shù)轉(zhuǎn)換成字符串

總結(jié)

以上是生活随笔為你收集整理的c语言中将整数转换成字符串_在C语言中将ASCII字符串(char [])转换为八进制字符串(char [])...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。