Jump to content
43oh

Int To Hexa ?


Recommended Posts

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
Link to post
Share on other sites

 

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];
Link to post
Share on other sites

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.

Link to post
Share on other sites

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.)
Link to post
Share on other sites

 Pointer? What pointer? :smile:

 

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

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?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...