Label displaying SysexData

How can I get a label displaying the Sysex data I’m receiving through a midiin port ?
I’m trying to build an array of the SysexData … but sending this to a text String is not working ?

[code]char anArray[100] ;

for (int iii = 0 ; iii <= message.getSysExDataSize() ; iii++)
	{
		ptr++ ; 
		anArray[iii] = *ptr ;
		String text = anArray;
		label4->setText(text, false);
	
		}
	
[/code]

I can’t quite see how you’re expecting the data to be formatted. Even of you wanted to make this into a (mostly garbage) string you’d need to null terminate the C string before constructing the Juce String. You also need to be careful updating GUI items from other threads (e.g., the midi thread).

String midiData;
midiData =String::toHexString(message.getRawData(),message.getRawDataSize()).removeCharacters(" ");

Looks like
f000201f0046200000040d04100503f7
but you can also keep the spacing for a view like this :
f0 00 20 1f 00 46 20 00 00 04 0d 04 10 05 03 f7

Salvator

Salvator: no need to remove the spaces manually, toHexString has a parameter that tells it how to add spaces to the result.

[quote=“Salvator”] String midiData; midiData =String::toHexString(message.getRawData(),message.getRawDataSize()).removeCharacters(" ");
Looks like
f000201f0046200000040d04100503f7
but you can also keep the spacing for a view like this :
f0 00 20 1f 00 46 20 00 00 04 0d 04 10 05 03 f7

Salvator[/quote]

Thanks Salavator, this works !

Now the Sysex data is representing ascii characters, is there an ascii equivalent to “toHexString” ?

I’ve gone through the Juce String class and the things I found are :

String (const char *text, size_t maxChars) Creates a string from a string of 8-bit ascii characters.

and

copyToUTF8 (CharPointer_UTF8::CharType *destBuffer, int maxBufferSizeBytes) const noexcept Copies the string to a buffer as UTF-8 characters.

But I don’t now how to declare these things …

I’m doing something like this without result :

midiData =String::createStringFromData(message.getRawData(),message.getRawDataSize()) ; const char* midiData2 = midiData.toUTF8() ; String text (midiData2) ; label4->setText(text, false);

Can “createStringFromData” handle the binary nature of the sysex data ? toHexString does ? But then I need to convert hex to ascii ?

I thought this should work but it doesn’t … that’s why I’m looking for workarrounds like above …

[code]String midiData ;
midiData =String::createStringFromData(message.getRawData(),message.getRawDataSize()) ;
label4->setText(midiData, false);

[/code]

this is what i do for sysex data, it includes both DEC and HEX displays

const String formatMidiMessage (const MidiMessage &message)
{
	if (logDecimal)
	{
		String ret;
		uint8 *ptr = message.getRawData();
		for (int i=0; i<message.getRawDataSize(); i++)
		{
			ret << String::formatted ("%.3d", (int)*(ptr+i));
			ret << " ";
		}
		return (ret.trim());
	}
	else
	{
		return (String::toHexString (message.getRawData(), message.getRawDataSize()));
	}
}
1 Like

Thanks Atom, but how would you display Ascii characters out of sysex data ? The sysex data contains for the most part Ascii characters .
I thought the “createStringFromData” would give me those characters .

you can use %c instead of %d but be careful i did that in my program and you will get crashes if you try to show non-printable characters, i use a special function before i print any sysex data as ascii


static const char *asciiSpecials[]	= {"[NUL]", "[SOH]", "[STX]", "[ETX]", "[EOT]", "[ENQ]", "[ACK]", "[BEL]", "[BS]", "[HT]", "[LF]", "[VT]", "[FF]", "[CR]", "[SO]", "[SI]", "[DLE]", "[DC1]", "[DC2]", "[DC3]", "[DC4]", "[NAK]", "[SYN]", "[ETB]", "[CAN]", "[EM]", "[SUB]", "[ESC]", "[FS]", "[GS]", "[RS]", "[US]", 0};

const String removeInvalidChars(const String &dataToValidate, const bool showSpecials, const char characterToReplace)
{
	String ret;
	StringArray specials(asciiSpecials);

	for (int i=0; i<dataToValidate.length(); i++)
	{
		juce_wchar c = dataToValidate[i];

		if (c < 0x20 && c != 0xd && c != 0x0b && c != 0x09 && c != 0x0a )
		{
			if (showSpecials)
			{
				ret << specials[(int)c];
			}
			else
			{
				ret << characterToReplace;
			}
		}
		else
		{
			ret << c;
		}
	}

	return (ret);
}

I’ll check that out Atom ! I just discoverd the “%.c” thing myself and this works although I still need to remove the non ascii part of the sysex , but for the moment these are not causing a crash …