2

I'm currently trying to convert a .NET JSON Encoder to NETMF but have hit a problem with Convert.ToString() as there isn't such thing in NETMF.

The original line of the encoder looks like this:

Convert.ToString(codepoint, 16);

And after looking at the documentation for Convert.ToString(Int32, Int32) it says it's for converting an int32 into int 2, 8, 10 or 16 by providing the int as the first parameter and the base as the second.

What are some low level code of how to do this or how would I go about doing this?

As you can see from the code, I only need conversion from an Int32 to Int16.

EDIT

Ah, the encoder also then wants to do:

PadLeft(4, '0');

on the string, is this just adding 4 '0' + '0' + '0' + '0' to the start of the string?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Infiniti Fizz
  • 1,726
  • 4
  • 24
  • 40
  • Could I potentially do something with Convert.toInt16(string s) as in turn my codepoint into a string with codepoint.ToString() and then use Convert.ToInt16(codepoint)? – Infiniti Fizz Jun 18 '12 at 21:26
  • 1
    Padding is conditionally adding `'0'` to the start of the string, such that the total length of the string is always the value `4` or greater. – NominSim Jun 18 '12 at 21:29
  • What do you mean with `As you can see from the code I only need conversion from an Int32 to Int16.` As far as i can see, it is related with converting an int to a base16-string representation like `31`=>`"1F"`, not `int16 j = (int16)i32`; – L.B Jun 18 '12 at 21:36
  • Ah sorry yes, I was just hunting around the .netmf stuff and lost track of what it was doing, I am trying to turn an Int32 into a base16 string. – Infiniti Fizz Jun 18 '12 at 21:44

2 Answers2

6

If you mean you want to change a 32-bit integer value into a string which shows the value in hexadecimal:

string hex = intValue.ToString("x");

For variations, please see Stack Overflow question Convert a number into the hex value in .NET.

Disclaimer: I'm not sure if this function exists in NETMF, but it is so fundamental that I think it should.

Community
  • 1
  • 1
cdkMoose
  • 1,646
  • 19
  • 21
  • 1
    Per [Expert .NET Micro Framework](http://www.windowsfordevices.com/files/misc/Kuhner.NETMicroFramework_Ch4_sample.pdf#page=20): “The .NET Micro Framework does not give you a possibility to convert [numbers to hexadecimal] by using the "X" format with the `ToString` method. [Doing so] would cause an `ArgumentException`.” – Douglas Jun 18 '12 at 22:29
  • Surprising, since it supports other ToString() methods which would be more complex, for example DateTime.ToString() – cdkMoose Jun 19 '12 at 12:25
  • Looking at this code comment: https://github.com/netmf/netmf-interpreter/blob/dev/Framework/Subset_of_CorLib/System/Number.cs#L108-L1115, it seems like latest μ-F/W does support `x` and `X` formatters. – vulcan raven May 08 '16 at 10:10
2

Here’s some sample code for converting an integer to hexadecimal (base 16):

int num = 48764;   // assign your number

// Generate hexadecimal number in reverse.
var sb = new StringBuilder();
do
{
    sb.Append(hexChars[num & 15]);
    num >>= 4;
} 
while (num > 0);

// Pad with leading 0s for a minimum length of 4 characters.
while (sb.Length < 4)
    sb.Append('0');

// Reverse string and get result.
char[] chars = new char[sb.Length];
sb.CopyTo(0, chars, 0, sb.Length);
Array.Reverse(chars);
string result = new string(chars);

PadLeft(4, '0') prepends leading 0s to the string to ensure a minimum length of 4 characters.

The hexChars value lookup may be trivially defined as a string:

internal static readonly string hexChars = "0123456789ABCDEF";

Edit: Replacing StringBuilder with List<char>:

// Generate hexadecimal number in reverse.
List<char> builder = new List<char>();
do
{
    builder.Add(hexChars[num & 15]);
    num >>= 4;
}
while (num > 0);

// Pad with leading 0s for a minimum length of 4 characters.
while (builder.Count < 4)
    builder.Add('0');

// Reverse string and get result.
char[] chars = new char[builder.Count];
for (int i = 0; i < builder.Count; ++i)
    chars[i] = builder[builder.Count - i - 1];
string result = new string(chars);

Note: Refer to the “Hexadecimal Number Output” section of Expert .NET Micro Framework for a discussion of this conversion.

Douglas
  • 53,759
  • 13
  • 140
  • 188
  • 1
    Why is `hexChars` a dictionary rather an array? – harold Jun 18 '12 at 21:42
  • I was about to ask that because there is no Dictionary collection in .net micro framework :-( but thanks so far for the awesome help! – Infiniti Fizz Jun 18 '12 at 21:45
  • 1
    Also there is no StringBuilder, I'm using the NETMF.CommonExtensions library someone made that has a version of SB in it but it is very limited and doesn't contain Length, I guess I can just create a char[] instead of an SB and then reverse it manually using a for loop, would that do the same as your code? – Infiniti Fizz Jun 18 '12 at 21:49
  • Okay not a char[] but a char arraylist – Infiniti Fizz Jun 18 '12 at 21:49
  • ArrayList but can't define a type so I've done this: used ArrayList.Add() then after the dowhile, copied the arraylist to a char[] with CopyTo() then created another new char[] called 'reverse' then go round a for loop assigning the reverse array [i++] to the original array [j--]. That'd work right? – Infiniti Fizz Jun 18 '12 at 21:54
  • Sounds like it should work. If you want to avoid the extra code required for reversing, just replace all `.Add(x)` occurrences with `.Insert(0, x)`, and the result will be constructed in correct order in-place. – Douglas Jun 18 '12 at 21:56
  • Ah okay yeah that makes sense. So with the PadLeft, if I do while(charsArrayList.Count < 4) { charsArrayList.Insert(0, '0'); } that'll basically do the same as PadLect(4, '0'); right? – Infiniti Fizz Jun 18 '12 at 22:01
  • 1
    Correct. I’d originally used `Add` because it’s more efficient, but in this case, the difference in performance will be negligible since your `ArrayList` will only be a few characters long. – Douglas Jun 18 '12 at 22:03
  • Ah okay cool. Thanks so much for the help Douglas, that was the last bit I needed to sort out :) – Infiniti Fizz Jun 18 '12 at 22:14