How to convert char array to hex or binary

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

How to convert char array to hex or binary

#1 Post by xpolhecx » 13 Dec 2011 20:28

Hi

I need to convert unsigned char buttons[8] to either

1) binary
2) hex
3) decimal

Any ideas?

Code: Select all

unsigned char buttons[8] = {'1','0','0','1','1','1','0','1'};

// send as binary
usb_write_buffer[0] = 0b10011101;

// send as hex
usb_write_buffer[1] = 0x9D;

// send as decimal
usb_write_buffer[2] = 240;

while(!HID_Write(&usb_write_buffer,3));
Binary = 10011101
Decimal = 240
Hex = 0x9D
Last edited by xpolhecx on 13 Dec 2011 20:51, edited 1 time in total.

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: How to convert char array to hex or binary

#2 Post by Sparky1039 » 13 Dec 2011 20:51

To convert a string literal numeric character subtract dec 48 from each to get the pure binary value of 0x00 and 0x01 respectively.
USBtx_Buff = buttons[x] - 48;

xpolhecx
Posts: 110
Joined: 24 Feb 2011 16:09

Re: How to convert char array to hex or binary

#3 Post by xpolhecx » 13 Dec 2011 21:45

Thank you for your response :) It works

Yako
Posts: 32
Joined: 11 Dec 2011 23:23

Re: How to convert char array to hex or binary

#4 Post by Yako » 14 Dec 2011 07:36

Difficult, resourceful, quirky and you wouldn't do it.

Post Reply

Return to “mikroC PRO for PIC General”