473,327 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

2 char's to 1 integer - and back.

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
Mar 29 '07 #1
13 1820
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]
- mv*@spam.guard.caspershouse.com

"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...
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

Mar 29 '07 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:u6**************@TK2MSFTNGP05.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...
>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


Mar 29 '07 #3
"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...
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...
Mar 29 '07 #4
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]
- mv*@spam.guard.caspershouse.com

"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:u6**************@TK2MSFTNGP05.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...
>>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



Mar 29 '07 #5
My shot. This?

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

"Mantorok" <no**@none.comha scritto nel messaggio
news:eu**********@newsfeed.th.ifl.net...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:u6**************@TK2MSFTNGP05.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...
>>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



Mar 29 '07 #6
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 3 '07 #7
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:6E**************@TK2MSFTNGHUB02.phx.gbl...
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
Apr 3 '07 #8
On Apr 3, 10:55 am, "Mantorok" <n...@none.comwrote:
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

Apr 3 '07 #9

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
On Apr 3, 10:55 am, "Mantorok" <n...@none.comwrote:
>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.
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
Apr 3 '07 #10
On Apr 3, 12:55 pm, "Mantorok" <n...@none.comwrote:
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.
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

Apr 3 '07 #11

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11********************@e65g2000hsc.googlegrou ps.com...
On Apr 3, 12:55 pm, "Mantorok" <n...@none.comwrote:
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.

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
Apr 3 '07 #12
On Apr 3, 3:00 pm, "Mantorok" <n...@none.comwrote:
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.
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

Apr 3 '07 #13
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

Mantorok wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
>On Apr 3, 10:55 am, "Mantorok" <n...@none.comwrote:
>>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.

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

Apr 4 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lucas | last post by:
hello one and all, i am very new to java and i need to take an ascii character, say "A" or "a" and convert it into its ascii integer code, say 65 or 97 respectively. then i need to also go in...
4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
13
by: Vijay Kumar R. Zanvar | last post by:
Hello, I have few questions. They are: 1. Is "const char * const *p;" a valid construct? 2. How do I align a given structure, say, at 32-byte boundary? 3. Then, how do I assert that a given...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
3
by: Ling Chen Wu | last post by:
Hi, I need to call a dll function but have no idea how to declare the function in VB.NET In the .h file, it is declare as follows: int CALLBACK gscBsiGcReadTagList( IN ...
3
by: pamelafluente | last post by:
Hi what is the smartest function that maps the char on the left to the char on the right (I want to complement an hexadecimal string, like making a negative color). 0 -f 1 -e 2...
43
by: TheDrunkenDead | last post by:
Hello, just wondering how I would assign a char array of four elements to the four bytes used in an int. As of right now my code is: cNameSize = (char)((void)NameSize); cFileSize =...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.