get dec value from a hex

so i read and read but i don't understand

let me tell

i have a Array with values

i read one value

ex
buffer , this return FF
but now i wanne have the dec value of FF = 255
how can i do that
i tried
long Ro = strtol(buffer*, NULL, 16);*
but get error
can someone bring me on the right direction

We PRINT values in decimal, hex, octal etc but the valuse never change they are values ( numbers) and there not in hex or dec thats just the way we write them

Mark

Is what you receive this:
char buf[] = "FF";
or this:
int number = 0xFF;

Do you want the number to be this:
char buf[] = "255";
or this:
int number = 255;

This seems like a simple question once things are understood. You might also want to ask in the language specific forums below to get help in your native language.

i receive this

char buf[] = "FF";

and wanne need this

int number = 255;.

something like this

int t0 = buf[0] - '0';
if (t0 >= 10) t0 -= 7;
int t1 = buf[1] - '0';
if (t1 >= 10) t1 -= 7;
int number = t1 *16 + t2;

You can also use the strtol() library function.

Indeed. If that's what you have, this should have worked:

long Ro = strtol(buffer, NULL, 16);

What error did you get?

On an Arduino 'strtol' is an incredibly long and inefficient function. You can do this though:

inline byte hexToByte(char* hex){
  byte high = hex[0];
  byte low = hex[1];
  if (high > '9'){
    high -= (('A'-'0')-0xA); //even if hex is lower case (e.g. 'a'), the lower nibble will have the correct value as (('a'-'A')&0x0F) = 0.
  }
  if (low > '9'){
    low -= (('A'-'0')-0xA); //even if hex is lower case (e.g. 'a'), the lower nibble will have the correct value as (('a'-'A')&0x0F) = 0.
  }
  return (((high & 0xF)<<4) | (low & 0xF));
}

inline unsigned int hexToInt(char* hex){
  return ((hexToByte(hex) << 8) | hexToByte(hex+2));
}
inline unsigned long hexToLong(char* hex){
  return ((((unsigned long)hexToInt(hex)) << 16) | hexToInt(hex+4));
}

Which can be called for example like this;

char buf[] = "FF";
byte number = hexToByte(buf);

//or
char bigbuf[] = "DEAD";
unsigned int largeNumber = hexToInt(bigbuf);

//or
char hugebuf[] = "DEADBEEF";
unsigned long hugeNumber = hexToLong(hugebuf);

thanks , i will try

I've just edited my last post with a better version which won't change the original string.

receive this error
standv31:71: error: invalid conversion from 'char' to 'char*'
standv31:71: error: initializing argument 1 of 'unsigned int hexToInt(char*)'

this is the code where it happens

Serial.print("-");
                      Serial.print(buffer[i]);
           Serial.print("-");
                   unsigned int largeNumber = hexToInt(buffer[i]);
                      Serial.print(largeNumber);                
           Serial.println("-");

Show me how you declare 'buffer'. (and preferably all of your code)

here is the code

it read data from a SD card

#include <SD.h>
#include <elapsedMillis.h>
#include <TimerOne.h>
#include "LPD6803.h"

const int chipSelect = 4;
char  buffer[516];
int dataPin = 2;       // 'yellow' wire
int clockPin = 3;  
LPD6803 strip = LPD6803(20, dataPin, clockPin);

void setup()
{
  
  
   strip.setCPUmax(100);
   strip.begin();
   strip.show();
   
  Serial.begin(115200);
  
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  while (!SD.begin(chipSelect)) {
    delay(250);
  }
  digitalWrite(LED_BUILTIN, LOW);
}

void loop()
{
  play();
 
}

void play()
{
  File f = SD.open("COREPLAY.TXT");
  if (f) {
   
    long period = f.parseInt();
    Serial.print("Period is ");
    Serial.println(period);
    f.readBytesUntil('\n', buffer, sizeof(buffer));
      elapsedMillis msec=0;
    
    while (f.available()) {
      f.readBytesUntil('\n', buffer, sizeof(buffer));
      Serial.print("Data: ");
      Serial.print(buffer);
      int channels = hex2bin(buffer);
      Serial.print(", ");
      Serial.print(channels);
      Serial.println(" channels");
      if (channels > 0) {
    
      
        for (int i=0; i < channels; i++) {
          
           //byte Ro = hexToByte(buffer[i]);
           //byte Gr = hexToByte(buffer[i++]);
           //byte Bl = hexToByte(buffer[i++]);
           int c = 0;
           //c = Color(Ro,Gr,Bl);
           //strip.setPixelColor(i, c);
           Serial.print("-");
                      Serial.print(buffer[i]);
           Serial.print("-");
                   unsigned int largeNumber = hexToInt(buffer[i]);
                      Serial.print(largeNumber);                
           Serial.println("-");
           
           
                 
           
           
           
        }
      
        strip.show();
        // wait for the required period
        while (msec < period) ; // wait
        msec = msec - period;
      }
    }
    f.close();
  }
}


byte hexdigit(char c)
{
  if (c >= '0' && c <= '9') return c - '0';
  if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  return 255;
}


int hex2bin(char *buf)
{
  byte b1, b2;
  int i=0, count=0;
  
  while (1) {
    b1 = hexdigit(buf[i++]);
    if (b1 > 15) break;
    b2 = hexdigit(buf[i++]);
    if (b2 > 15) break;
    buf[count++] = b1 * 16 + b2;
    }
  return count;
}

unsigned int Color(byte r, byte g, byte b)
{
  //Take the lowest 5 bits of each value and append them end to end
  return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
}

inline byte hexToByte(char* hex){
  byte high = hex[0];
  byte low = hex[1];
  if (high > '9'){
    high -= (('A'-'0')-0xA); //even if hex is lower case (e.g. 'a'), the lower nibble will have the correct value as (('a'-'A')&0x0F) = 0.
  }
  if (low > '9'){
    low -= (('A'-'0')-0xA); //even if hex is lower case (e.g. 'a'), the lower nibble will have the correct value as (('a'-'A')&0x0F) = 0.
  }
  return (((high & 0xF)<<4) | (low & 0xF));
}

inline unsigned int hexToInt(char* hex){
  return ((hexToByte(hex) << 8) | hexToByte(hex+2));
}
inline unsigned long hexToLong(char* hex){
  return ((((unsigned long)hexToInt(hex)) << 16) | hexToInt(hex+4));
}

it's a copy paste code

Yeah, that's not how you would use it.

You'd need to do something like this:
unsigned int largeNumber = hexToInt(buffer+i);

i have some values now , but not what i expected

one line of data is like this

00FF0000FF0000FF000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

i need values for

00
FF
00

and so on

So you would need something like this:

for (int i=0; i < channels; i++) { 
           Serial.print("-");
           byte number = hexToByte(buffer[i<<1]);
           Serial.print(number);                
           Serial.println("-");
}

get error in line

byte number = hexToByte(buffer[i<<1]);

standv31.ino: In function 'void play()':
standv31:77: error: invalid conversion from 'char' to 'char*'
standv31:77: error: initializing argument 1 of 'byte hexToByte(char*)'

Yeah, sorry about that, I modified the code you had posted and forgot to change a bit of it. Try this:

for (int i=0; i < channels; i++) { 
           Serial.print("-");
           byte number = hexToByte(&buffer[i<<1]);
           Serial.print(number);                
           Serial.println("-");
}

hmm ,

also strange values and not what it should be . . .

otherwise i can with a python script generate a dec value file with , as delimiter

but then i need to store every value into a array

i mean

data = "255,255,0,128,2,255" and so on

then then array will look like

storeddata[1] = 255
storeddata[2] = 255
storeddata[3] = 0
storeddata[4] = 128

and so on

i already read that i need strok.

if this way wanne be easier it's no problem for me.