C / C ++如何将int转换回字符串(一个单词)?

时间:2022-09-02 09:16:35

In C I'm reading a string from a txt file, for example the string "hello", so i have my:

在C中我正在从txt文件中读取一个字符串,例如字符串“hello”,所以我有我的:

F=fopen("text.txt","r");
fscanf(F,"%s\n",string);

Now, if i want to convert this string to hex and decimal, i can do something like this:

现在,如果我想将此字符串转换为十六进制和十进制,我可以这样做:

for (i=0; i<strlen(string); i++)
{
   sprintf(st, "%02X", string[i]);  //Convert to hex
   strcat(hexstring, st);

   sprintf(st, "%03d", string[i]);  //Convert to dec
   strcat(decstring, st);
}

Now, my question is: i want to do the inverse operation, but how? This is the output if i convert "hello"

现在,我的问题是:我想做逆操作,但是怎么做?如果我转换“你好”这是输出

   hex-> 68656c6c6f
   dec-> 104101108108111

From "68656c6c6f" or "104101108108111" i want to go back to "hello", how can i do this?

从“68656c6c6f”或“104101108108111”我想回到“你好”,我该怎么做?

(basically i want to do something like this website: http://string-functions.com/; String To Hex Converter, Hex To String Converter, Decimal To Hex, Converter, Hex To Decimal Converter)

(基本上我想做这样的网站:http://string-functions.com/;字符串到十六进制转换器,十六进制到字符串转换器,十进制到十六进制,转换器,十六进制到十进制转换器)

1 个解决方案

#1


The challenge is to realize you have a string of hex and a string of decimal, meaning you have a character string representation of the values, not the values themselves. So you will need to convert the string representations to the appropriate numeric values before converting back to the original string.

挑战是要意识到你有一个十六进制字符串和一个十进制字符串,这意味着你有一个字符串表示值,而不是值本身。因此,在转换回原始字符串之前,您需要将字符串表示转换为适当的数值。

Presuming you have hex as a string representation of two-byte hex character pairs, the following will return the original string hello from 68656c6c6f:

假设你有十六进制作为双字节十六进制字符对的字符串表示,以下将从68656c6c6f返回原始字符串hello:

/* convert hex string to original */
char hex2str[80] = {0};
char *p = hex2str;
int i = 0;
int itmp = 0;

while (hex[i])
{
    sscanf (&hex[i], "%02x", &itmp);
    sprintf (p, "%c", itmp);
    p++;
    i+=2;
}
*p = 0;

printf ("\n hex2str: '%s'\n\n", hex2str);

Output

$ ./bin/c2h2c < <(printf "hello\n")

 hex2str: 'hello'

Short Working Example

#include <stdio.h>
#include <string.h>

#define MAXS 64

int main (void) {

    char hex[] = "68656c6c6f";
    char hex2str[MAXS] = {0};
    char *p = hex2str;
    int itmp = 0;
    int i = 0;

    /* convert hex string to original */
    while (hex[i])
    {
        sscanf (&hex[i], "%02x", &itmp);
        sprintf (p, "%c", itmp);
        p++;
        i+=2;
    }
    *p = 0;

    printf ("\n hex2str: '%s'\n\n", hex2str);

    return 0;
}

Output

$ ./bin/c2h2c

 hex2str: 'hello'

#1


The challenge is to realize you have a string of hex and a string of decimal, meaning you have a character string representation of the values, not the values themselves. So you will need to convert the string representations to the appropriate numeric values before converting back to the original string.

挑战是要意识到你有一个十六进制字符串和一个十进制字符串,这意味着你有一个字符串表示值,而不是值本身。因此,在转换回原始字符串之前,您需要将字符串表示转换为适当的数值。

Presuming you have hex as a string representation of two-byte hex character pairs, the following will return the original string hello from 68656c6c6f:

假设你有十六进制作为双字节十六进制字符对的字符串表示,以下将从68656c6c6f返回原始字符串hello:

/* convert hex string to original */
char hex2str[80] = {0};
char *p = hex2str;
int i = 0;
int itmp = 0;

while (hex[i])
{
    sscanf (&hex[i], "%02x", &itmp);
    sprintf (p, "%c", itmp);
    p++;
    i+=2;
}
*p = 0;

printf ("\n hex2str: '%s'\n\n", hex2str);

Output

$ ./bin/c2h2c < <(printf "hello\n")

 hex2str: 'hello'

Short Working Example

#include <stdio.h>
#include <string.h>

#define MAXS 64

int main (void) {

    char hex[] = "68656c6c6f";
    char hex2str[MAXS] = {0};
    char *p = hex2str;
    int itmp = 0;
    int i = 0;

    /* convert hex string to original */
    while (hex[i])
    {
        sscanf (&hex[i], "%02x", &itmp);
        sprintf (p, "%c", itmp);
        p++;
        i+=2;
    }
    *p = 0;

    printf ("\n hex2str: '%s'\n\n", hex2str);

    return 0;
}

Output

$ ./bin/c2h2c

 hex2str: 'hello'