Converting a byte value into 2 ASCII value

Hi, I have searched high and low. I have solve most of it but I cant seem to solve one last bit :confused:

  1. I have successful map a joystick to give me my Zoom Speed value from 0 to 63 into HEX (0x00 to 0x3F).

  2. I was also able to split the value which I gotten in (1) into two separate part.
    For example the HEX value 3F into 3 and F.

  3. However I am unable to fully map the part I gotten in (2) into individual HEX value. I would need to map the ASCII value of 3 --> 33 and value of F -->46. For the value of F, I keep getting 3F as the HEX value. I would want to get 46. This happens for A,B,C,D,E,F only. For all other numbers, it is fine.

Please help. :slight_smile:

Code:
byte ZoomSpeed = map(ZoomOutval,0,zoomOutRange, 63, 0);

//Split Zoom Speed value into 2. For example 3F will become 3 and F
byte zparts1 = ZoomSpeed>>4;
byte zparts2 = ZoomSpeed & 0xF;

//Convert the ASCII value each value into HEX Value
zparts1 = zparts1 + '0';
zparts2 = zparts2 + '0' ;

Serial.print("Joystick value is ");
Serial.println(zVal);
Serial.print("Zoom speed is ");
Serial.println(ZoomSpeed, HEX);
Serial.println(zparts1,HEX);
Serial.println(zparts2,HEX);

emu84:
Hi, I have searched high and low. I have solve most of it but I cant seem to solve one last bit :confused:

  1. I have successful map a joystick to give me my Zoom Speed value from 0 to 63 into HEX (0x00 to 0x3F).

  2. I was also able to split the value which I gotten in (1) into two separate part.
    For example the HEX value 3F into 3 and F.

  3. However I am unable to fully map the part I gotten in (2) into individual HEX value. I would need to map the ASCII value of 3 --> 33 and value of F -->46. For the value of F, I keep getting 3F as the HEX value. I would want to get 46. This happens for A,B,C,D,E,F only. For all other numbers, it is fine.

Please help. :slight_smile:

not sure why what you are trying to achieve but something like this should be able to 'split the value'

byte ZoomOutval, zoomOutRange = 100; //donno the actual value since you did not post the full code!

void setup() {

  Serial.begin(115200);

  byte ZoomSpeed = map(ZoomOutval, 0, zoomOutRange, 63, 0);

  //Split Zoom Speed value into 2. For example 3F will become 3 and F
  char zparts[3];
  sprintf(zparts, "%.2X", ZoomSpeed);

  Serial.print("Joystick value is ");
  Serial.println(ZoomOutval);
  Serial.print("Zoom speed is ");
  Serial.println(ZoomSpeed, HEX);
  Serial.println(zparts[0]);
  Serial.println(zparts[1]);

}

void loop() {

}

hope that helps....

Thank you! Problem solved :slight_smile: