oliveiracarlos 2 Posted January 24, 2013 Share Posted January 24, 2013 Please, people... I am taking the analog value from potentiometer as Long Int. I need to change this value to Hexa ? thanks. Quote Link to post Share on other sites
cubeberg 540 Posted January 24, 2013 Share Posted January 24, 2013 Please, people... I am taking the analog value from potentiometer as Long Int. I need to change this value to Hexa ? thanks. Do you mean Hex? Quote Link to post Share on other sites
oliveiracarlos 2 Posted January 24, 2013 Author Share Posted January 24, 2013 Yes Hex, sorry ! decimal 127 = 7F hex thanks. Quote Link to post Share on other sites
cubeberg 540 Posted January 24, 2013 Share Posted January 24, 2013 Are you looking to convert it to hex in a string or to display somehow? The int value itself isn't specifically decimal or hex - those are just ways of displaying a certain value. tripwire 1 Quote Link to post Share on other sites
oliveiracarlos 2 Posted January 24, 2013 Author Share Posted January 24, 2013 I created a function that needs a hex value as parameter. The potentiometer value is always int (0...1023). All my code is below. As I need a value from 0..127 as SendSysex function parameter, I get the potentiometer value and applying ldiv(value, 8); But now I need this value (0..127) as hex to send to function. Thanks a lot. PS: I am brazilian, so the functions comments are in portuguese. PS2: I am usign 2553 rev 1.5 #include <math.h>int sensor1 = 0;long int sensor1Valor = 0;void setup() { Serial.begin(31250);}void loop() { sensor1Valor = f1024t8(analogRead(A3)); SendSysex(0x1F, 0x00, 0x20, 0x77, sensor4Valor); delay(1);}long int f1024t8(long int f1024){ ldiv_t res; res = ldiv(f1024, 8); return res.quot;}void SendSysex(long int addr1, long int addr2, long int addr3, long int addr4, long int data){ //soma os valores long int addrMaisData = 0; addrMaisData = addr1 + addr2 + addr3 + addr4 + data; //calcula o resto da divis Quote Link to post Share on other sites
oPossum 1,083 Posted January 24, 2013 Share Posted January 24, 2013 void uint32hex(uint32_t x, char *s) { unsigned n = 8; s += n; *s-- = 0; do { *s-- = "0123456789ABCDEF"[x & 15]; x >>= 4; } while(--n); } oliveiracarlos, mbeals, cubeberg and 2 others 5 Quote Link to post Share on other sites
oliveiracarlos 2 Posted January 24, 2013 Author Share Posted January 24, 2013 void uint32hex(uint32_t x, char *s) { unsigned n = 8; s += n; *s-- = 0; do { *s-- = "0123456789ABCDEF"[x & 15]; x >>= 4; } while(--n); } Thanks a lot my friend, I will try it. Quote Link to post Share on other sites
bluehash 1,581 Posted January 24, 2013 Share Posted January 24, 2013 void uint32hex(uint32_t x, char *s) { unsigned n = 8; s += n; *s-- = 0; do { *s-- = "0123456789ABCDEF"[x & 15]; x >>= 4; } while(--n); } Holy hell.. I'm going to save this. Opossum.. would you mind explaining this snippet. I've never see a line like this: *s-- = "0123456789ABCDEF"[x & 15]; Quote Link to post Share on other sites
cubeberg 540 Posted January 24, 2013 Share Posted January 24, 2013 Holy hell.. I'm going to save this. Opossum.. would you mind explaining this snippet. I've never see a line like this: *s-- = "0123456789ABCDEF"[x & 15]; That's the kind of pointer stuff that makes my head hurt :!!!: Quote Link to post Share on other sites
oPossum 1,083 Posted January 24, 2013 Share Posted January 24, 2013 That's the kind of pointer stuff that makes my head hurt :!!!: Pointer? What pointer? static const char * const hex = "0123456789ABCDEF"; *s-- = hex[x & 15]; static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; *s-- = hex[x & 15]; Same thing. bluehash 1 Quote Link to post Share on other sites
pabigot 355 Posted January 24, 2013 Share Posted January 24, 2013 Same thing.Almost. The array form will save one or two bytes because it doesn't require an end-of-string character the way the string representation does. Remember sizeof("1234") is five, not four. (The EOS is one; a second might be saved depending on how things get padded when the data is laid out, viz. if the following data must be word-aligned.) Quote Link to post Share on other sites
bluehash 1,581 Posted January 24, 2013 Share Posted January 24, 2013 Same thing. I understand now. Thanks Quote Link to post Share on other sites
cubeberg 540 Posted January 24, 2013 Share Posted January 24, 2013 Pointer? What pointer? static const char * const hex = "0123456789ABCDEF"; *s-- = hex[x & 15]; static const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; *s-- = hex[x & 15]; Same thing. I meant the *s stuff. Tried that a couple of times in CCS based on some examples of going through a string array - didn't work for some reason. Quote Link to post Share on other sites
tripwire 139 Posted January 25, 2013 Share Posted January 25, 2013 Hi Oliveira, oPossum's uint32hex function is indeed a good way to convert a number to a hex string, but I've been looking at your code and it doesn't appear that you need to do this. As cubeberg said: "The int value itself isn't specifically decimal or hex - those are just ways of displaying a certain value." If I have a function like this: void test(int i) { // insert code here... } I can call it in any of these ways: test(12345); // decimal integer literal test(0x3039); // hexadecimal integer literal test(030071); // octal integer literal (!) int number = someFunctionThatReturns12345(); test(number); // integer variable All with the same result. So as long as analogRead(A3) returns an int in the range 0-1023 the code you posted above should work correctly. That is, unless there's some other function that you want to pass the potentiometer value to which is expecting hexadecimal in a character string. Quote Link to post Share on other sites
roadrunner84 466 Posted January 25, 2013 Share Posted January 25, 2013 So.... where do you need to "convert to hex"? I see you writing a lot of bytes in the 0xAB form, but that is just the representation, sending 0x7F is the same as sending 127; both are the same number. The only place where you actually would need to convert to hex, is where you'll need to print (or write human readable over serial) numbers which must be hexedecimally represented, I failed to find such a requirement in your code. I mean, you are actually sending bytes which are not human readable, so the rest of the data in between doesn't have to be human readable either, right? tripwire 1 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.