Input - String : Output - Binary Oct Dec Hex

M

Markus Sommer

Hello!

string test = char(2) + "HelloWorld" + char(29);

IsPrintableCharToDec(test);

My target:
Example
Output <2>HelloWorld<29>

public string IsPrintableCharToDec(char c)
{
string ret= c.ToString();
if ((!char.IsLetterOrDigit(c) &&
!char.IsPunctuation(c)) &&
!char.IsSymbol(c))
{
ret = "<" + c.ToString() + ">";
}
return ret;
}


Output <2>HelloWorld<1D>

public string IsPrintableCharToHex(char c)
{
string ret= c.ToString();
if ((!char.IsLetterOrDigit(c) &&
!char.IsPunctuation(c)) &&
!char.IsSymbol(c))
{
ret = "<" + c.ToString() + ">";
}
return ret;
}

http://en.wikipedia.org/wiki/American_Standard_Code_for_Information_Interchange
Binary Oct Dec Hex Abbr PR[t 1] CS[t 2] CEC[t 3] Description
000 0000 000 0 00 NUL †^@ \0 Null character
000 0001 001 1 01 SOH â ^A Start of Header
000 0010 002 2 02 STX â‚ ^B Start of Text
000 0011 003 3 03 ETX ⃠^C End of Text


How can I reach my goal?

Greeting Markus
 
M

Markus Sommer

Hello Patrice,

Patrice said:
Hello,

Try :
http://msdn.microsoft.com/en-us/library/b1kwkfdz.aspx

(assuming your problem is the converting to base 2, 8 or 16).

yes works well.
Is that or know you another way?

Maybe GetByteConverter? I'm not sure.

Greeting Markus


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class Program
{
public static string IsPrintableChar(char c)
{
string ret = c.ToString();
if ((!char.IsLetterOrDigit(c) &&
!char.IsPunctuation(c)) &&
!char.IsSymbol(c))
{
char number2 = c; // (char)29;
int baseValue2 = 10;
Console.WriteLine(" {0,-8} --> 0x{1}", number2,
Convert.ToString(number2, baseValue2));

ret = "<" + Convert.ToString(number2, baseValue2) + ">";
}
return ret;
}

//http://msdn.microsoft.com/en-us/library/b1kwkfdz.aspx
//http://msdn.microsoft.com/en-us/library/system.componentmodel.byteconverter.aspx

static void Main(string[] args)
{

int[] bases = { 2, 8, 10, 16 };
short[] numbers = { Int16.MinValue, -13621, -18, 12, 19142,
Int16.MaxValue };

foreach (int baseValue in bases)
{
Console.WriteLine("Base {0} conversion:", baseValue);
foreach (short number in numbers)
{
Console.WriteLine(" {0,-8} --> 0x{1}",
number, Convert.ToString(number,
baseValue));
}
}

Console.WriteLine("IsPrintableChar {0}",
IsPrintableChar((char)29));
Console.ReadKey();
}
}
}
 
P

Patrice

If it works stick to it.. This is a lower level infrastructure for type
conversion and I'm not even sure it supports using a base.

--
Patrice

Markus Sommer said:
Hello Patrice,

Patrice said:
Hello,

Try :
http://msdn.microsoft.com/en-us/library/b1kwkfdz.aspx

(assuming your problem is the converting to base 2, 8 or 16).

yes works well.
Is that or know you another way?

Maybe GetByteConverter? I'm not sure.

Greeting Markus


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Test
{
class Program
{
public static string IsPrintableChar(char c)
{
string ret = c.ToString();
if ((!char.IsLetterOrDigit(c) &&
!char.IsPunctuation(c)) &&
!char.IsSymbol(c))
{
char number2 = c; // (char)29;
int baseValue2 = 10;
Console.WriteLine(" {0,-8} --> 0x{1}", number2,
Convert.ToString(number2, baseValue2));

ret = "<" + Convert.ToString(number2, baseValue2) + ">";
}
return ret;
}

//http://msdn.microsoft.com/en-us/library/b1kwkfdz.aspx
//http://msdn.microsoft.com/en-us/library/system.componentmodel.byteconverter.aspx

static void Main(string[] args)
{

int[] bases = { 2, 8, 10, 16 };
short[] numbers = { Int16.MinValue, -13621, -18, 12, 19142,
Int16.MaxValue };

foreach (int baseValue in bases)
{
Console.WriteLine("Base {0} conversion:", baseValue);
foreach (short number in numbers)
{
Console.WriteLine(" {0,-8} --> 0x{1}",
number, Convert.ToString(number,
baseValue));
}
}

Console.WriteLine("IsPrintableChar {0}",
IsPrintableChar((char)29));
Console.ReadKey();
}
}
}
 
A

Arne Vajhøj

For simple cases see the code below.

Arne

====================================

private static string DIGITS = "0123456789ABCDEF";
private static int FromAny(string s, int radix)
{
int res = 0;
char[] sa = s.ToCharArray();
for (int i = 0; i < s.Length; i++) {
res = res * radix + DIGITS.IndexOf(sa);
}
return res;
}
private static string ToAny(int i, int radix)
{
string res = "";
int tmp = i;
while (tmp > 0) {
res = DIGITS.ToCharArray()[tmp % radix] + res;
tmp = tmp / radix;
}
return res;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top