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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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…