Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
ifor38special
Posts: 17
Registered: ‎07-23-2010
My Device: Blackberry Storm 9530

C Sharp has ByteArray.ToString("x2") - Whats BB JME Equivalent?

I'm converting a C Sharp Application and came across this in a GetHexString Method:

 

ByteArray.ToString("x2");  

 

Which if you notice the "x2" say's to make two bytes of Hex.  

Someone suggest ByteArrayUtilities  but the methods there don't appear to make a X2 Hex String.

Does anyone have some Plain Java code or Blackberry Java (<OS6) that will do the equivalent to

this C Sharp Line?

 

Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: C Sharp has ByteArray.ToString("x2") - Whats BB JME Equivalent?

Can you describe what this C sharp statement actually does?

Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: C Sharp has ByteArray.ToString("x2") - Whats BB JME Equivalent?

Assuming that you have the data in a java byte[], you'll probably have to invent this by looping through the bytes (casting the int), then use Integer.toHexString().

 

You'll probably have put a little code in there to add the leading a zero where the legth of the resulting hex string is 1 character.

 

Please use plain text.
Contributor
ifor38special
Posts: 17
Registered: ‎07-23-2010
My Device: Blackberry Storm 9530

Re: C Sharp has ByteArray.ToString("x2") - Whats BB JME Equivalent?

Here it is in J#

Notice at the bottom the X2 as a format into the Byte.ToString("x2");  

I need a JME (Blackberry) to do the same thing with Classes. I'm using OS 7 on torch.

 

// Example for the Byte.ToString( ) methods.
import System.* ;
import System.Globalization.* ;

class ByteToStringDemo
{
    static void RunToStringDemo()
    {
        ubyte smallValue = 13;
        ubyte largeValue = 234;

        // Format the Byte values without and with format strings.
        Console.WriteLine("\nIFormatProvider is not used:");
        Console.WriteLine("   {0,-20}{1,10}{2,10}", "No format string:", 
            (UInt16)smallValue, (UInt16)largeValue);
        Console.WriteLine("   {0,-20}{1,10}{2,10}", "'X2' format string:", 
            String.Format("{0:X2}", (UInt16)smallValue), 
            String.Format("{0:X2}", (UInt16)largeValue));

        // Get the NumberFormatInfo object from the 
        // invariant culture.
        CultureInfo culture = new CultureInfo("");
        NumberFormatInfo numInfo = culture.get_NumberFormat();

        // Set the digit grouping to 1, set the digit separator
        // to underscore, and set decimal digits to 0.
        numInfo.set_NumberGroupSizes(new int[] { 1 });
        numInfo.set_NumberGroupSeparator("_");
        numInfo.set_NumberDecimalDigits(0);

        // Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine(("\nA NumberFormatInfo object with "
            + "digit group size = 1 and \ndigit separator = '_' is used for the"
            + " IFormatProvider:"));
        Console.WriteLine("   {0,-20}{1,10}{2,10}", "No format string:",
            System.Convert.ToString(smallValue, numInfo), 
            System.Convert.ToString(largeValue, numInfo));

        //create two object arrays for passing to String.Format()
        Object smallArr[] = { (Int32)smallValue };
        Object largeArr[] = { (Int32)largeValue };

        Console.WriteLine("   {0,-20}{1,10}{2,10}", "'N' format string:", 
            String.Format(numInfo, "{0:N}", smallArr), 
            String.Format(numInfo, "{0:N}", largeArr));
    } //RunToStringDemo

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of\n" + "   Byte.ToString( ),\n"  
            + "   Byte.ToString( String ),\n" 
            + "   Byte.ToString( IFormatProvider ), and\n" 
            + "   Byte.ToString( String, IFormatProvider )\n" 
            + "generates the following output when formatting " 
            + "Byte values \nwith combinations of format " 
            + "strings and IFormatProvider."));
        RunToStringDemo();
    } //main
} //ByteToStringDemo

/*
This example of
   Byte.ToString( ),
   Byte.ToString( String ),
   Byte.ToString( IFormatProvider ), and
   Byte.ToString( String, IFormatProvider )
generates the following output when formatting Byte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:           13       234
   'X2' format string:         0D        EA

A NumberFormatInfo object with digit group size = 1 and
digit separator = '_' is used for the IFormatProvider:
   No format string:           13       234
   'N' format string:         1_3     2_3_4
*/
 
Please use plain text.
Contributor
ifor38special
Posts: 17
Registered: ‎07-23-2010
My Device: Blackberry Storm 9530

Re: C Sharp has ByteArray.ToString("x2") - Whats BB JME Equivalent?

http://msdn.microsoft.com/en-us/library/y11056e9(v=vs.80).aspx

Is where you can read about C Sharp  Byte.ToString("x2");

 

 

Please use plain text.
Developer
mreed
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: C Sharp has ByteArray.ToString("x2") - Whats BB JME Equivalent?

Please use plain text.