2 char's to 1 integer - and back.

  • Thread starter Thread starter Mantorok
  • Start date Start date
M

Mantorok

Hi

Is it possible to convert 2 char values to an integer and be able to reverse
it?

I'm sure there must be some algorithm to achive this.

Any ideas?
Thanks
Kev
 
Kev,

Do you mean '1' and '0' to produce the integer 10?

If so, just create a new string instance and then pass it to the static
Parse method on the Int32 class.

To reverse the operation, call ToString on the int, and then call
ToChars on the string instance to get the characters.

Hope this helps.
 
Sorry, I should've been clearer.

I mean if I had 'A' and 'B', trying to convert them to integers and then
back.

Kev

Nicholas Paldino said:
Kev,

Do you mean '1' and '0' to produce the integer 10?

If so, just create a new string instance and then pass it to the static
Parse method on the Int32 class.

To reverse the operation, call ToString on the int, and then call
ToChars on the string instance to get the characters.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mantorok said:
Hi

Is it possible to convert 2 char values to an integer and be able to
reverse it?

I'm sure there must be some algorithm to achive this.

Any ideas?
Thanks
Kev
 
Is it possible to convert 2 char values to an integer and be able to
reverse it?

I'm sure there must be some algorithm to achive this.

Let's say the two chars in question are 'a' and 'Z'...

char[] charArray = {'a', 'Z'};

int int1 = (int)charArray[0]; // 97
int int2 = (int)charArray[1]; // 90

string string1 = int1.ToString(); // "97"
string string2 = int2.ToString(); // "90"

string strFinal = string1 + string2 + string1.Length; // "97902"
int intFinal = int.Parse(strFinal); // 97902


The final digit, in this case 2, is the "check-digit" i.e. it tells you
where to split the "string"...

So, to reverse it, convert it to a string, split it at the check digit, then
convert to to integer values back to chars...
 
Kev,

To assign to an int:

int a = 'a';

To assign an int to a character.

char c = (char) a;


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mantorok said:
Sorry, I should've been clearer.

I mean if I had 'A' and 'B', trying to convert them to integers and then
back.

Kev

Nicholas Paldino said:
Kev,

Do you mean '1' and '0' to produce the integer 10?

If so, just create a new string instance and then pass it to the
static Parse method on the Int32 class.

To reverse the operation, call ToString on the int, and then call
ToChars on the string instance to get the characters.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mantorok said:
Hi

Is it possible to convert 2 char values to an integer and be able to
reverse it?

I'm sure there must be some algorithm to achive this.

Any ideas?
Thanks
Kev
 
My shot. This?

char a1 = 'A', a2 = 'B';
int Packeda12 = (a1 << 8) + a2;
char UnpackedA2 = (char)(a2 & (0xFFFF));
char UnpackedA1 = (char)(Packeda12 >> 8);

Mantorok said:
Sorry, I should've been clearer.

I mean if I had 'A' and 'B', trying to convert them to integers and then
back.

Kev

Nicholas Paldino said:
Kev,

Do you mean '1' and '0' to produce the integer 10?

If so, just create a new string instance and then pass it to the
static Parse method on the Int32 class.

To reverse the operation, call ToString on the int, and then call
ToChars on the string instance to get the characters.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mantorok said:
Hi

Is it possible to convert 2 char values to an integer and be able to
reverse it?

I'm sure there must be some algorithm to achive this.

Any ideas?
Thanks
Kev
 
Hi Kev,

I think you want to obtain the ASCII integer value of the 2 chars and
convert them back. Then, you may just static const them between "int" and
"char" just as Nicholas provided. Does this meet your need? If you still
need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
"Jeffrey Tan[MSFT]" said:
Hi Kev,

I think you want to obtain the ASCII integer value of the 2 chars and
convert them back. Then, you may just static const them between "int" and
"char" just as Nicholas provided. Does this meet your need? If you still
need any help, please feel free to feedback, thanks.

Hi, again I should've explained better, my resulting integer is limited to
999, silly I know but this is the constraint I'm working against.

I personally think it will be impossible as the chars are made up of 2
values and the integer is only 1.

Kev
 
Hi, again I should've explained better, my resulting integer is limited to
999, silly I know but this is the constraint I'm working against.

I personally think it will be impossible as the chars are made up of 2
values and the integer is only 1.

Well, that's not the problem. The problem is that even if you limit
yourself to A-Z, a-z, 0-9 for each of the characters, that's still 62
characters, giving 62*62=3844 possible combinations - you can't
represent that in the integers 0-999.

Jon
 
Jon Skeet said:
Well, that's not the problem. The problem is that even if you limit
yourself to A-Z, a-z, 0-9 for each of the characters, that's still 62
characters, giving 62*62=3844 possible combinations - you can't
represent that in the integers 0-999.

Well, it's one of the problems, but yes you are right, the other problem is
there is no way to map the number of possible combinations, which is another
approach that could've been taken.

Kev
 
Well, it's one of the problems, but yes you are right, the other problem is
there is no way to map the number of possible combinations, which is another
approach that could've been taken.

No, the fact that you would need to combine 2 values into 1 isn't a
problem at all - that can be done trivially, eg by effectively
bitshifting the first character's 16-bit Unicode value by 16, then
"OR"-ing it with the second Unicode value. It's far from impossible.
Without the "0-999" restriction, there would be no issues. The 0-999
restriction makes it impossible.

Jon
 
Jon Skeet said:
No, the fact that you would need to combine 2 values into 1 isn't a
problem at all - that can be done trivially, eg by effectively
bitshifting the first character's 16-bit Unicode value by 16, then
"OR"-ing it with the second Unicode value. It's far from impossible.
Without the "0-999" restriction, there would be no issues. The 0-999
restriction makes it impossible.

How would this be achieved? I would be interested to know as it's not
something I've had to do in the past.

Kev
 
How would this be achieved? I would be interested to know as it's not
something I've had to do in the past.

Try this:

uint first = firstChar;
uint second = secondChar;

uint unsigned = (first<<16) | second;
int signed = (int) unsigned;

On the way back (given "signed"):

uint unsigned = (uint) signed;
ushort first = (ushort) (signed>>16);
ushort second = (ushort) (signed & 0xffff);

char firstChar = (char)first;
char secondChar = (char)second;

I suspect I'm being a bit overly cautious about signed-ness, but I
don't have time to check right now :)

Jon
 
Yes if you limit the character set as specified you can actually pack 3
characters per integer. It is a techniques called Rad50 that the old
PDP11 assemblers used to use for speed. I wrote a cross assemble many
years ago (early 90s i think) that duplicated the method.

Following is some really bad C code i wrote long ago before I knew how
to be a better programmer. Hopefully it helps you. I actually combine
two 32 bit words to make one long int. This way one long compare checks
6 characters. The long word served as a key in a btree.

#define getrad50(x) ((long)(rad50table[(uchar)x]))
/* Rad50 coversion table */
char rad50table[256] =
{
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,27,-1,-1,-1,
-1,-1,-1,-1,-1,-1,28,-1,30,31,
32,33,34,35,36,37,38,39,-1,-1,
-1,-1,-1,-1,-1, 1 ,2, 3, 4, 5,
6, 7, 8, 9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,
26,-1,-1,-1,-1,29,-1, 1, 2, 3,

4, 5, 6, 7, 8, 9,10,11,12,13,
14,15,16,17,18,19,20,21,22,23,
24,25,26,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,

-1,-1,-1,-1,-1
};

/* get next characters and put as worksym */
void getworksym()
{
char *cptr = currentchar;
short j;
ushort r50a = 0,r50b = 0;

/* get first 3 characters */
if ((j = (short)getrad50(*cptr++)) >= 0)
{
r50a = j * 1600;
if ((j = (short)getrad50(*cptr++)) >= 0)
{
r50a += j * 40;
if ((j = (short)getrad50(*cptr++)) >= 0)
{
r50a += j;
/* get second 3 characters */
if ((j = (short)getrad50(*cptr++)) >= 0)
{
r50b = j * 1600;
if ((j = (short)getrad50(*cptr++)) >= 0)
{
r50b += j * 40;
if ((j = (short)getrad50(*cptr++)) >= 0)
{
r50b += j;
for (;getrad50(*cptr++) >= 0;);
}
}
}
}
}
}
worksym = ((long)r50a << 16) | r50b; /* make long rad50 character */
getnextchar(--cptr);/* get next non white space */
}

Hope this helps
Leon Lambert
 

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

Back
Top