Convert long to base 32 string in Wiring

Hi all

The Firmware documentation says you can do these number to string conversions:
String stringOne = String(45, HEX);
String stringOne = String(analogRead(0), DEC);

But I want to convert a Long value to a base 32 string. In Java I can do this:
Long value = 251415;
String valueAsString = Long.toString( value, 32 );

Is there an equivalent function in Wiring? That is how do I convert a Long value to a base 32 string in Wiring,

Thanks in advance
PQ

You could try the C function void itoa(int input, char *buffer, int radix)

On these 32bit µC it’s the same as ltoa() and lltoa() could be used for 64bit long long numbers.

1 Like

My purpose in moving to base 32 string encoding is so I can cram more information into the 63 bytes of an event.

I can now answer my own question. On the server side I am using these javascript procedures to do the encoding/decoding:

  • value.toString(32)
  • parseInt( value, 32 )

In Wiring I use these procedures to do the same thing:

String numToString32( long value ) 
{
  String answer = "";

  if( value == 0 )
	answer = "0";
  else
  {
	boolean negative = ( value < 0 );
	if( negative)
		value = - value;
	
	while( value > 0 )
	{
		int remainder = (int) ( value % 32 ); //(value - ( value / 32 ) );
		value = value / 32;

		char theChar = theAlphabet.charAt( remainder );
		answer = theChar + answer;
	}

	if( negative)
		answer = "-" + answer;
  }

  return answer;
}

long string32ToNum( String value )
{
  long answer = 0;
  boolean negative = false;

  unsigned int i = 0;

  for( ; i < value.length(); i++ )
  {
	char theChar = value.charAt(i);
	
	if( theChar == '-' )
		negative = true;
	
	// Strip of any preceding minus signs or preceding zeros
	if( ( theChar != '-' ) && theChar != '0' )
		break;
  }

  for( ; i < value.length(); i++ )
  {
	char theChar = value.charAt(i);
	answer = answer*32 + theAlphabet.indexOf(theChar);
  }

  if( negative )
	answer = - answer;

  return answer;
}

I’ve compared the results of the above procedures against the standard Java / Javascript functions using “round-trip” test cases on a handful of values (positive, negative and zero). I’ve also tested the parsing of strings left-padded with “0”. I’m using it on my Core and server, and all seems to work with round-trip messaging. Lightly tested so far but looking good.

If I discover any bugs I’ll update this post.

2 Likes

Hmmm. The procedure numToString32 stopped working at some stage after I posted it. I suspect (but can’t prove) compiler changes. I needed to modify it to get it working again. Here is the revised code:

const String theAlphabet = "0123456789abcdefghijklmnopqrstuvw";

// Convert a base 32 encoded string to a long. Mirrors behaviour of Java function Long.parseLong( value, 32 );
String numToString32( long value )
{
	String answer = "";
	
	if( value == 0 )
		answer = "0";
	else
	{
		boolean negative = ( value < 0 );
		if( negative)
			value = - value;
		
		while( value > 0 )
		{
			int remainder = (int) ( value % 32 );
			answer = theAlphabet.substring( remainder, remainder+1 ) + answer;

			value = value / 32;
		}

		if( negative)
			answer = "-" + answer;
	}
	
	return answer;
}