-4

I need to set input value as decimal

int data = 2000;

and then, output as hexadecimal string or char format:

hexValue = 0x7D0;

then need to extract the value in following format:

char hexdata[]= {0x07, 0xD0};

How can this be achieved by writing a C program? I am using KEIL MDK-5 IDE. Any idea?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Farhan
  • 5
  • 1

4 Answers4

3

try following

int data=2000;
char hexdata[4];
printf("hexValue = %x", data);
hexdata[3] = data & 0xFF;
hexdata[2] = (data>>8) & 0xFF;
hexdata[1] = (data>>16) & 0xFF;
hexdata[0] = (data>>24) & 0xFF;

you can choose to write a for loop for extracting hexdata

Ashwani
  • 104
  • 5
  • This is the best option, since it is fully portable. [See this](http://stackoverflow.com/a/37433057/584518) for options how to make the code more rugged and safe. – Lundin Jun 02 '16 at 11:19
  • @Lundin disagree with "fully portable". 1) Right shifting a sign bit is implementation defined, 2) Many embedded platforms use 16-bit `int`. (As hinted in OP's `{0x07, 0xD0};`) Better to use `uint32_t data=2000;` as hinted in your reference. – chux - Reinstate Monica Jun 02 '16 at 14:29
  • @chux It is only implementation-defined if the sign bit is set. Good point about the size of int though. But then of course no professionals use `int` anyway, they all use the stdint types. – Lundin Jun 02 '16 at 15:27
  • @Ashwani, Could you please let me understand more about your codes above. As you have written such that what is the possible wat to implement that in this format if, int a = 2000; then hex = 0x7D0 how can I achieve like this --> char hexdata[]={0x07, 0xD0}; Your shown way, that something I couldnt understand how to follow after that. – Farhan Jun 03 '16 at 03:53
  • @Farhan, I could help you better if you explain what your objective is? In fact your question is not very much clear what you intend to do. I assumed that you have an int variable and you want to extract each byte of it. My code assume that the underlying platform has int size of 4-byte (it could be different on different platforms as pointed by chux) hence I took four byte array hexdata and later brought each byte of int variable in lower 8-bits and did a 'logical and' operation to get it in hexdata array – Ashwani Jun 03 '16 at 06:21
  • @chux, what is your point while referring "1) Right shifting a sign bit is implementation defined"? If you want to say that a right shift will lead to different MSBs for shifting out bits depending upon the implementation still it won't affect the hexdata values extracted from int variable as the "and operation" will mask all those bits – Ashwani Jun 03 '16 at 06:27
  • @Ashwani, I beleive I explained well that What I actually want it from what value. To rephrase, The integer value converts the 16 bits-hex and the hex value would split 8-bit and 8-bit. and then the first 8-bit (considering from LSB) will place in one variable and second 8-bit (considering MSB) in another variable. Anyhow, I have achieved the result. Your Code was helpful to initiate the idea but not exactly works that well as What exactly I want it. Finally, I made another way. And, Big THANKS to all. – Farhan Jun 03 '16 at 07:06
  • Fair point concerning the sign bit. – chux - Reinstate Monica Jun 03 '16 at 14:25
0

Unfortunately, I couldn't understand what do you need really, If you want to represent one byte of a decimal value in hex format at a time, you can use the following code or something like that.

int main(void)
{
    int a = 2000;
    unsigned char *p = (unsigned char *)&a;
    int i;

    printf("a =");
    for (i = 0; i < sizeof(a); ++i)
    {
        printf(" %02x", p[i]);
    }
    printf("\n");
    return 0;
}
Adonaim
  • 170
  • 1
  • 2
  • 11
  • This is not a good idea, since the code is endianess dependent. – Lundin Jun 02 '16 at 11:18
  • I meant, the actual format in the program to place the value is in this format. char hexdata[]={0x07, 0xD0); // which means the 16-bit hex value of 0x7D0 and decimal value of 2000; I want input the decimal value as 2000 and then first it should convert it to corresponding hex value then the hexvalue must be formated in following manner 0x7D0 --> 0x07, 0xD0 After, Need to place those values char hexdata[] = {0x07, 0xD0} – Farhan Jun 03 '16 at 03:46
0

If this works for you.

int main(void)
{
    int a = 2000;
    char str[8];
    int i;

    sprintf(str, "%X", a);
    // now you have the number as a character in str variable. you can seperate them using string functions now.
    return 0;
}
  • Thanks It may work, but this is to convert Decimal to Hex. and then I will get this variable "str" as an hex value 0x7D0 of int a=2000; But I need to extract that hex value of 0x7D0 into 0x07 and 0xD0 – Farhan Jun 03 '16 at 03:48
  • for your comment. This works well and I usually follow snprintf instead sprintf. I actually need help after this process as I achieved with this command already. Anyhow, Thanks for your support. and to inform, got the output. Let me close the conversation. Thanks again. – Farhan Jun 03 '16 at 07:09
0

Easiest way to do number system conversions is using strtol function. Of course, you need to:

  1. Convert decimal number into string. Please, read more about that here: How to convert integer to string in C?

  2. Use strol function to get hexadecimal value.

From man pages:

long int strtol(const char *nptr, char **endptr, int base);

long long int strtoll(const char *nptr, char **endptr, int base);

The strtol() function converts the initial part of the string in nptr to a long integer value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).

The remainder of the string is converted to a long int value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter 'A' in either uppercase or lowercase represents 10, 'B' represents 11, and so forth, with 'Z' representing 35.)

If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.

The strtoll() function works just like the strtol() function but returns a long long integer value.

Community
  • 1
  • 1
Aleksandar Makragić
  • 1,957
  • 17
  • 32