1

Well I have been trying to convert this integer into hex and have successfully done so but I need to use this hex for setting something. Now for this I need to use a char not a char array. Nothing else has worked without manually setting it. Maybe the problem lies in the issue that I use sprintf for the conversion to hex but either way I am sure there is a way to complete this task. Now What I need to change is have the output be char z but I haven't found a way to get this to work. Any help is greatly appreciated. Thanks

EDIT: now thi code may not make sense directly because it is incomplete and I saw no purpose inputting unrelated code. int x will never be over 100 and the whole point is to convert this into a hex and write it to the memory of a setting I have. So I have been trying to figure out how to convert the integer into hex into a char. nonstring as someone pointed out even though sprintf converts it to a string stored in a char as I just noticed. But I need to take the int convert to hex and assign that to a char variable forbuse later on. And that is where I am stuck. I do not know the best way to go about completely all that in a format and way without going into a string and other things.

VOID WriteSetting(int x)
{
    char output[8];
    sprintf(output, "0x%X", x);
    char z = 0x46
    unsigned char y = z
}

Working Code:

VOID WriteSetting(int x)
{
    unsigned char y = (unsigned char)x;
    Settingdb.Subset.Set = y;
}
Kebob
  • 23
  • 1
  • 4
  • I don't understand this. What's all this with the conversion, the char array, the char, the int...? –  Jun 06 '13 at 05:55
  • 2
    There is a huge difference between `0x46` and `"0x46"` – Karthik T Jun 06 '13 at 05:56
  • 1
    Be very careful when using sprintf, your output buffer is only 4 chars long, so if x is above 255 your going to be in trouble. A bit of string theory perhaps wouldn't go amiss here. If you're using C++ look into std::string, it will solve many problems. – Cramer Jun 06 '13 at 06:04
  • This code doesn't make sense - the output buffer will likely be overrun, but the contents are local to the function regardless. The same is true of `z` and `y`. 0x46 is 'F' in ASCII, but what's the significance of it? – Brett Hale Jun 06 '13 at 06:11
  • I am writing this to a very specific setting on hardware that is not regularly used sobthis need to be specific. it will take in a int not hex from another function I have but again this is irrevolent. I just need to convert the int to hex assign it to a nonstring char and then done. I do not know why it is so difficult to do a simple task like this in C and Cplusplus. Maybe I am missing something. – Kebob Jun 06 '13 at 13:42

4 Answers4

0

Have you tried

printf("0x%X", z);
Karthik T
  • 31,456
  • 5
  • 68
  • 87
0

While the C++ standards do not explicitly require it, the size of a character is sometimes a byte, and an integer 4 bytes. I presume that this is why you are using an array of 4 characters.

So when you have an integer type and try to cram it into a single character, you'll lose precision.

P.S. A more precise explanation of sizes of data types is here: What does the C++ standard state the size of int, long type to be?

Community
  • 1
  • 1
Kirby
  • 3,649
  • 3
  • 26
  • 28
0

If what you want is for:

WriteSetting(70);

or

WriteSetting(0x46);

to do the same thing as

char z = 0x46;
unsigned char y = z;

then all you need to do is:

void WriteSetting(int x)
{
    unsigned char y = x;
}

Integers don't have any inherent base - they're just numbers. There's no difference at all between unsigned char y = 0x46; and unsigned char y = 70;.

caf
  • 233,326
  • 40
  • 323
  • 462
  • so if I intake 70 and set it to a char then will that convert to hexadecimal? I need it to be a hexadecimal or else the setting fails and crashes the hardware I am working with. so although there is no difference between the two, the srtting intake reads differently – Kebob Jun 06 '13 at 14:10
  • 1
    I say again - *integers don't have any inherent base*. If `unsigned char y = 0x46;` works for your hardware, then `unsigned char y = 70;` will also work, because it is *exactly the same thing*. – caf Jun 06 '13 at 22:41
  • This worked! Awesome thanks for the useful information. I just used some different code but using just unsigned char worked so thank you very much. – Kebob Jun 07 '13 at 00:41
0
void WriteSetting(unsigned char *y, int x){
    if(x > 100){
        fprintf(stderr, "x value(%d) is invalid at %s\n", x, __func__);
        return ;
    }
    *y = x;//There is no need for conversion probably
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70