-2

I have char array like

char* str = 0x058f;

What should i do to convert this value to int and uint16_t and print correctly this value by uint16_t type?

That mean i want the uint16_t result is 058f as well:

 uint16_t result = 0x058f;
Tiktac
  • 966
  • 1
  • 12
  • 32

2 Answers2

2

You can use strtol to convert string to integer value

*char* str = "0x058f";
result = strtol(str,NULL,16);
printf("result = %x\n",result);*

I hope this helps.

Sami
  • 311
  • 2
  • 8
1

convert this value to int and uint16_t and print correctly this value by uint16_t

int main(void) {

  char* str = "0x058f";

  int i;
  unsigned u;
  if (sscanf(str, "%x", &u) != 1) 
    Handle_Failure();
  i = (int) u;

  uint16_t u16;
  if (sscanf(str, "%" SCNx16, &u16) != 1) 
    Handle_Failure();

  printf("i: 0x%04x  i16: 0x%04" PRIx16 "\n", i, u16);
  return 0;
}

i: 0x058f  i16: 0x058f
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256