[英]C parse dec to hex and output as char[]
I need some help please. 我需要一些帮助。 I am looking to modify the DecToHex function.
我想修改DecToHex函数。
For input decimalNumber = 7 : 对于输入decimalNumber = 7:
Actual output : 实际输出:
sizeToReturn = 2;
hexadecimalNumber[1] = 7;
hexadecimalNumber[0] = N/A ( is garbage );
Desired output : 所需输出:
sizeToReturn = 3
hexadecimalNumber[2] = 0
hexadecimalNumber[1] = 7
hexadecimalNumber[0] = N/A ( is garbage )
The function : 功能 :
void DecToHex(int decimalNumber, int *sizeToReturn, char* hexadecimalNumber)
{
int quotient;
int i = 1, temp;
quotient = decimalNumber;
while (quotient != 0) {
temp = quotient % 16;
//To convert integer into character
if (temp < 10)
temp = temp + 48; else
temp = temp + 55;
hexadecimalNumber[i++] = temp;
quotient = quotient / 16;
}
(*sizeToReturn) = i;
}
This will append each u8 to an array :
这会将每个u8附加到数组:
for (int k = size - 1;k > 0;k--)
AppendChar(Str_pst, toAppend[k]);
You are really close, you can reverse in the array and add a '0'
to the beginning with little effort, or you can leave it the way you have it and take care of it in main. 您真的很亲密,可以不费吹灰之力就可以在数组中倒置并在开头添加一个
'0'
,或者可以按原样保留它并在主目录中进行处理。 Where I think you are getting wound around the axle is in the indexing of hexadecimalNumber
in your function. 我认为您会受到困扰的地方是函数中
hexadecimalNumber
的索引。 While 7
produces one hex-digit, it should be at index zero in hexadecimalNumber
(except you initialize i = 1
) That sets up confusion in handling your conversion to string indexes . 虽然
7
产生一个十六进制数字,它应该是在指数为零 hexadecimalNumber
(除非你初始化i = 1
),设置了混乱,处理转换为字符串的索引 。 Just keep the indexes straight, initializing i = 0
and using hexadecimalNumber
initialized to all zeros, if you only have a single character at index 1
, pad the string with 0
at the beginning. 只需保持索引笔直,初始化
i = 0
并使用hexadecimalNumber
初始化为全零,如果您在索引1
处只有一个字符,请在字符串开头添加0
。
Here is a short example that may help: 这是一个简短的示例,可能会有所帮助:
#include <stdio.h>
#include <stdlib.h>
#define NCHR 32
void d2h (int n, char *hex)
{
int idx = 0, ridx = 0; /* index & reversal index */
char revhex[NCHR] = ""; /* buf holding hex in reverse */
while (n) {
int tmp = n % 16;
if (tmp < 10)
tmp += '0';
else
tmp += '7';
revhex[idx++] = tmp;
n /= 16;
}
if (idx == 1) idx++; /* handle the zero pad on 1-char */
while (idx--) { /* reverse & '0' pad result */
hex[idx] = revhex[ridx] ? revhex[ridx] : '0';
ridx++;
}
}
int main (int argc, char **argv) {
int n = argc > 1 ? atoi (argv[1]) : 7;
char hbuf[NCHR] = "";
d2h (n, hbuf);
printf ("int : %d\nhex : 0x%s\n", n, hbuf);
return 0;
}
The 0x
prefix is just part of the formatted output above. 0x
前缀只是上面格式化输出的一部分。
Example Use/Output 使用/输出示例
$ ./bin/h2d
int : 7
hex : 0x07
$ ./bin/h2d 26
int : 26
hex : 0x1A
$ ./bin/h2d 57005
int : 57005
hex : 0xDEAD
If you do want to handle the reversal in main()
so you can tack on the 0x07
if the number of chars returned in hexadecimalNumber
are less than two, then you can do something similar to the following: 如果您确实想在
main()
处理逆转,因此如果hexadecimalNumber
返回的字符数少于两个,则可以添加0x07
,那么您可以执行以下操作:
void d2h (int n, int *sz, char *hex)
{
int idx = 0;
while (n) {
int tmp = n % 16;
if (tmp < 10)
tmp += '0';
else
tmp += '7';
hex[idx++] = tmp;
n /= 16;
}
*sz = idx;
}
int main (int argc, char **argv) {
int n = argc > 1 ? atoi (argv[1]) : 7, sz = 0;
char hbuf[NCHR] = "";
d2h (n, &sz, hbuf);
printf ("int : %d\nhex : 0x", n);
if (sz < 2)
putchar ('0');
while (sz--)
putchar (hbuf[sz]);
putchar ('\n');
return 0;
}
Output is the same 输出是一样的
Look it over and let me know if you have further questions. 查看一下,如果您还有其他问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.