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

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

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

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

Logic:

邏輯:

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

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

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

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

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

C程序將ASCII char []轉換為十六進制char [] (C program to convert ASCII char[] to hexadecimal char[])

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

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

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

Output

輸出量

ascii_str: Hello world!
hex_str: 48656C6C6F20776F726C6421

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

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

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

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

相關文章

redis rdb aof區別_理解Redis的持久化機制:RDB和AOF

什么是Redis持久化?Redis作為一個鍵值對內存數據庫(NoSQL)&#xff0c;數據都存儲在內存當中&#xff0c;在處理客戶端請求時&#xff0c;所有操作都在內存當中進行&#xff0c;如下所示&#xff1a;這樣做有什么問題呢&#xff1f;注 意文末有&#xff1a;3625頁互聯網大廠面…

python--批量下載豆瓣圖片

溜達豆瓣的時候&#xff0c;發現一些圖片&#xff0c;懶得一個一個扒&#xff0c;之前寫過c#和python版本的圖片下載&#xff0c;因此拿之前的Python代碼來改了改&#xff0c;折騰出一個豆瓣版本&#xff0c;方便各位使用 # -*- coding:utf8 -*- import urllib2, urllib, socke…

linux touch權限不夠,Linux下的Access、Modify、Change , touch的使用以及權限問題

每個文件在linux下面都會記錄許多的時間參數&#xff0c;其實是有三個主要的變動時間&#xff0c;那么&#xff0c;這三個時間的意義又是什么&#xff1f;下面我們來介紹&#xff1a;* Modify time(mtime)當該文件的“內容數據”更改時&#xff0c;就會更新這個時間。內容數據指…

scala 獲取數組中元素_從Scala中的元素列表中獲取隨機元素

scala 獲取數組中元素We can access a random element from a list in Scala using the random variable. To use the random variable, we need to import the Random class. 我們可以使用隨機變量從Scala中的列表訪問隨機元素。 要使用隨機變量&#xff0c;我們需要導入Rand…

ubuntu14.04下安裝cudnn5.1.3,opencv3.0,編譯caffe及配置matlab和python接口過程記錄

已有條件: ubuntu14.04cuda7.5anaconda2(即python2.7)matlabR2014a 上述已經裝好了,開始搭建caffe環境. 1. 裝cudnn5.1.3,參照:2015.08.17 Ubuntu 14.04cuda 7.5caffe安裝配置 詳情:先下載好cudnn-7.5-linux-x64-v5.1-rc.tgz安裝包(貌似需要官網申請) 解壓: tar -zxvf cudnn-7.…

python excel導入oracle數據庫_【Python代替Excel】12:Python操作oracle數據庫

日常工作中&#xff0c;如果有數據庫權限&#xff0c;那么在oracle中提取數據、在Python中處理是比較方便的。Python也提供了一個庫專門操縱數據庫。今天就專門來講講如何在Python中操作數據庫。準備工作需要工具&#xff1a;oracle、PL/SQL、Pythonimport cx_Oracle如果用anac…

Linux 金字塔 的shell命令,linux下保留文件系統下剩余指定數目文件的shell腳本

原文出處&#xff1a;http://www.jbxue.com/article/13808.html (原創文章&#xff0c;轉載請注明出處)本節內容&#xff1a;保留文件系統下剩余指定數目的文件例子&#xff1a;#!/bin/bash#-------------------------------#Description: Back up your files#site: www.jbxue.…

前端干貨之JS最佳實踐

持續更新地址 https://wdd.js.org/js-best-pr... 1. 風格 一千個讀者有一千個哈姆雷特&#xff0c;每個人都有自己的code style。我也曾為了要不要加分號給同事鬧個臉紅脖子粗&#xff0c;實際上有必要嗎&#xff1f; 其實JavaScript已經有了比較流行的幾個風格 JavaScript Sta…

python requests和urllib_Python——深入理解urllib、urllib2及requests(requests不建議使用?)...

深入理解urllib、urllib2及requestsPython 是一種面向對象、解釋型計算機程序設計語言&#xff0c;由Guido vanRossum于1989年底發明&#xff0c;第一個公開發行版發行于1991年&#xff0c;Python 源代碼同樣遵循 GPL(GNU General PublicLicense)協議[1] 。Python語法簡潔而清晰…

ssh查找linux端口,linux – 查找當前連接的端口號SSH

我正在使用SSH連接創建一個本地模擬器(未連接到Internet).我已經開始使用特定范圍的端口號進行sshd,并對一系列設備進行NAT處理.我必須找到當前連接的端口號.OS CentOS 5.5OpenSSH 6.1我做了以下事情.它適用于正常使用(手動用戶).但是當嘗試嚴格的測試(自動化)時,似乎有時找不到…

this.getstate_Java線程類Thread.State getState()方法(帶示例)

this.getstate線程類Thread.State getState() (Thread Class Thread.State getState()) This method is available in package java.lang.Thread.getState(). 軟件包java.lang.Thread.getState()中提供了此方法。 This method is used to return the state of this thread. 此方…

Java資源大全中文版(Awesome最新版)

來源&#xff1a;http://www.cnblogs.com/best/p/5876559.html 目錄 業務流程管理套件字節碼操作集群管理代碼分析編譯器生成工具構建工具外部配置工具約束滿足問題求解程序持續集成CSV解析數據庫數據結構時間日期工具庫依賴注入開發流程增強工具分布式應用分布式數據庫發布文檔…

運用多種設計模式的綜合案例_SpreadJS 純前端表格控件應用案例:表格數據管理平臺...

由某科技公司研發的表格數據管理平臺&#xff0c;是一款面向業務和企業管理系統定制開發的應用平臺&#xff0c;包括類 Excel 設計器、PC應用端和移動應用端等應用模塊。該平臺具備強大的業務配置和集成開發能力&#xff0c;對于企業客戶的信息系統在管理模式、業務流程、表單界…

linux定位哪個進程出發重啟,定位Linux下定位進程被誰KILL

hezhaoaqiang2012-11-09 11:10可以請教你一個問題嗎&#xff1f;關于arm的交叉編譯。我是按照&#xff1a;http://blog.chinaunix.net/uid-27003388-id-3276139.html 去做的&#xff0c;但是走到 四、建立初始編譯器(bootstrap gcc)下面的make install 它提示如下&#xff1a;m…

Java Integer類numberOfLeadingZeros()方法的示例

整數類numberOfLeadingZeros()方法 (Integer class numberOfLeadingZeros() method) numberOfLeadingZeros() method is available in java.lang package. 在java.lang包中提供了numberOfLeadingZeros()方法 。 numberOfLeadingZeros() method is used to returns the number o…

VS中C++ 項目重命名

應該都有過這樣的經歷&#xff0c;在Visual studio中創建解決方案&#xff0c;添加幾個項目進去&#xff0c;然后開始愉快的敲代碼...。寫代碼正歡的時候&#xff0c;卻總是感覺那里有些不舒服&#xff0c;一細看&#xff0c;這項目名稱取的真心挫&#xff0c;修改個吧。直接右…

Java GregorianCalendar getActualMinimum()方法與示例

GregorianCalendar類getActualMinimum()方法 (GregorianCalendar Class getActualMinimum() method) getActualMinimum() method is available in java.util package. getActualMinimum()方法在java.util包中可用。 getActualMinimum() method is used to get the actual minim…

axure9數據統計插件_WMDA:大數據技術棧的綜合實踐

一、概述WMDA是58自主開發的用戶行為分析產品&#xff0c;同時也是一款支持無埋點的數據采集產品&#xff0c;只需要在第一次使用的時候加載一段SDK代碼&#xff0c;即可采集全量、實時的PC、M、APP三端以及小程序的用戶行為數據。同時&#xff0c;為了滿足用戶個性化的數據采集…

Java Collections unmodifiableCollection()方法與示例

集合類unmodifiableCollection()方法 (Collections Class unmodifiableCollection() method) unmodifiableCollection() method is available in java.util package. unmodifiableCollection()方法在java.util包中可用。 unmodifiableCollection() method is used to get an un…

openfoam安裝中出現allmake error_如何更新OpenFOAM的版本?

這是協作翻譯的第四章&#xff0c;翻譯完感覺挺有意思的&#xff0c;分享給大家一起看看。4.更新OpenFOAM版本4.1 版本管理OpenFOAM以兩種不同的方式分發。一種方式是使用Git倉庫下載的倉庫版本。倉庫版本的版本號由附加的x標記&#xff0c;例如 OpenFOAM2.1.x。該版本會經常更…