Int to hex char (0xFF)


Hello ,I am using esp32 to read mp3 from sd card and send sound wave (-65535 to 65535)to a blue tooth speaker.I am now able to send a 6s array :const unsigned char {0xFF,….really large};
But i now have to decode mp3 ged get each sound value ,convert it to hex(like 4000 to 0xFA0).but I don’t know how to do.I tried,and i got this message ,please help ,thank you.
Luke lin

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

in this specific case, the code is assigning a c-string, array of chars, to a char, a single byte

    char piano16bit_raw = "";

Thank you,I understand!

Thanks, could you give me a example to convert 256 to 0xFF in char?(But i tried to use char not const char but also failed)

not const char. piano16bit_raw needs to be an array of char either piano16bit_raw [] or *piano16bit_raw

consider

output

 val 255, 0xff

code

    byte val = 255;
    char s [80];
    sprintf (s, " val %d, 0x%02x", val, val);
    Serial.println (s);

could you explan why i got output of:

 val 255, 0xff

with this code?

  Serial.begin(9600);
    byte val = 255;
    char s [80];
  //  sprintf (s, " val %d, 0x%02x", val, val);
    Serial.println (s);

I don't know why char s has val in it
it's not

char c [80] =val

Thank you

luke

You are declaring the array in a function and not defining its contents hence it will contain whatever was already in the memory that the compiler allocates to it

Wow,could you tell me how to get only 0xFF Without "val,255"?
thank you

Luke

I am sorry but I don't understand the question

Is this really 17 bit sound?

all values in memory are binary bits. they can be represented in various ways. they could be pixels, they can represent integer numbers of floats. integer value can be represented in various ways, as binary, octal, hex or decimal.

sprintf() formats variables into the c-string, s array in various ways. "%d" means decimal. "%02x" mean 2 digit hexadecimal with leading zeroes.

Hi,I will do some calculations to make it from 0 to 256 thanks

Surely, you mean 0 to 255?

I’m sorry, please take a look

I did it ,this works:


 Serial.begin(9600);
  //int dat=216;
    byte val = 216;
    char s [80];
    sprintf (s, "  0x%02x", val);
    Serial.println (s);

Well, I have got c==“0xFF”
But some thing is wrong
Code

Serial.begin(9600);
  //int dat=216;
    int val = audio.interleaved[0];
    char s [80];
    sprintf (s, "  0x%02x", val);
    Serial.println (s);
  SoundData *data = new OneChannelSoundData(s, sizeof(s));
  a2dp_source.write_data(data);//send wave data to BT speaker

warning:
invalid conversion from 'unsigned int' to 'ChannelInfo' [-fpermissive]

Please post the entire error listing, not a snippet.

Sorry, i can’t copy

Why? You should be able to. The full message is chopped off the screen.