|
Maybe something like that:
int Number = 57;
char szString[255];
strcpy(szString,"");
while (true)
{
if (Number>=27)
{
strcat(szString,"a");
Number -= 26;
}
else
{
char Temp;
Temp = Number + 97;
int StrLen = strlen(szString);
szString[StrLen] = Temp;
szString[StrLen+1] = '\0';
break;
}
}
Ok, I didn't tested it so there could be some mistakes but this gives you the general idea
|
|
|
|
|
you code won't work because you consider that if (Number>=27) only...
but if Number == 53 , the result must be "ba" , etc...
as P-Rex said, there are until "zzzz" possibilities, that means 26^4 = 456976 cases...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
I think you didn't understand completely my code
For exemple if Number == 53, the result must be "ab" and not "ba".
Thus in my code, if (Number >= 27), I DON'T jump out of the loop but comes once again in the loop, thus adding 'b' to the already existing string (that contains 'a').
Is it a little bit clearer ?
[EDIT] Sorry, I don't add 'b' but 'z' because 53-27 = 26 that corresponds to 'z'
[EDIT2] After 2 minutes of reflexion, I see that there effectively a problem in the code . There will only be 'a' in the begining of the string... I must not subtract but rather divide by 26... But I think the other solutions are good
|
|
|
|
|
You sound like you have it. But in case you haven't thought of this, you can imagine you are working with a base 27 system of numbers and you are converting from decimal.
Begin by checking if your value is greater than (27^3+1) if it is get the mod of your input number by (27^3+1 because a is 1), that's your first digit which falls between 0 and 26. then subtract that number off your working total. Then check if the remainder is bigger than (27^2+1). If it is, then mod by (27^2+1), that's your second digit. And so on. Since you are working with a-z just add the value of the char('a') to each mod and save yourself a switch statement. One for loop of four iterations should do it.
|
|
|
|
|
<br />
CString str;<br />
int number = 53;<br />
<br />
do<br />
{<br />
number--;<br />
str.Insert(0, 'a' + (number % 26));<br />
number /= 26;<br />
}<br />
while (number > 0);<br />
<br />
TRACE(_T("str: %s"), str);<br />
|
|
|
|
|
Thanks, you are my hero.
I didn´t thought that this is so easy
Pascal
|
|
|
|
|
As he properly realized, going from a number to a 'A' to 'ZZZZ' representation is the same as printing the number base 26 where the digits are the letters 'A' - 'Z'. If you wanted to write a number converter to print that value as decimal, then just change the / 26 to / 10 and the initial character from 'A' to '0'.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
<font color=blue>int</font> iVal = 453; <font color=green>
<font color=blue>char</font> Result[5] = <font color=gray>""</font>;
<font color=blue>if</font> (iVal >= 17577) { <font color=green>
Result[0] = (iVal % 26) - 1 + <font color=gray>'A'</font>;
}
<font color=blue>else</font> {
Result[0] = <font color=gray>'\0'</font>;
}
<font color=blue>if</font> (iVal >= 677) { <font color=green>
Result[1] = (iVal % 26) - 1 + <font color=gray>'A'</font>;
}
<font color=blue>else</font> {
Result[1] = <font color=gray>'\0'</font>;
}
<font color=blue>if</font> (iVal >= 27) { <font color=green>
Result[2] = (iVal % 26) - 1 + <font color=gray>'A'</font>;
}
<font color=blue>else</font> {
Result[2] = <font color=gray>'\0'</font>;
}
<font color=blue>if</font> (iVal >= 1) { <font color=green>
Result[3] = (iVal % 26) - 1 + <font color=gray>'A'</font>;
}
<font color=blue>else</font> {
Result[3] = <font color=gray>'\0'</font>;
}
Result[4] = <font color=gray>'\0'</font>;
<font color=green>
<font color=blue>for</font> (int</font> i = 0, i < 4, i++) {
<font color=blue>if</font> (Result[1] == <font color=gray>'\0'</font>) {
Result[i] = Result[i+1];
}
}
i didnot tested however...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
|
|
|
What value is represented by '0' ?
If you know that, then you have a base 26 system.
See, everything goes up to 26, then it carries over one place.
You are just mapping a base 26 numeric system to letters.
|
|
|
|
|
Sometimes when calling a method "BeginMessage" on an out of process COM component I get the error "The Server Threw an Exception".
I want to use SetUnhandledExceptionFilter in the COM component to set up my own handler "TopLevelHandler" for this error so that I can write a minidump file when it occurs.
My problem is that the handler I set up never gets triggered I just continue to receive the original error "The Server Threw an Exception".
Why wont the COM Component trigger my handler? Is it because there is another global handler that overrides mine? I've noticed that the debugger also never breaks in when the error occurs.
Something clever
|
|
|
|
|
I'm currently reading in a binary file which contains hexadecimal data, however I then need to convert this data from hexadecimal into binary (as well as swap the ordering from least significant bit to most significant bit, i.e. if I get (in hex) C9 6F in I need to swap these to 6F C9 and then convert it into binary, i.e. 0110 1111 1100 1001).
Does anyone know if there is a system call for doing this (or a standard algorithm). I've looks at htons (and other methods) but they rely on the data type being 'short' whereas the data read in from the file is stored as a 'char' in an array.
TIA,
Andy
|
|
|
|
|
Hi Andy,
How do you want to store the binary? So you want a string that reads "0110 1111 1100 1001"? or is it in a 16-bit variable?
As for swapping the bytes, store the bytes in array achOrig, then just bit shift them:
short int w16Bits = (achOrig[0] << 8) | achOrig[1];
Now you will have w16Bits = 6F C9 (in memory)
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Thanks for the bit shift example.
The binary data will eventually get displayed on the screen but we will also be processing the data depending on what type it was originally (i.e. integer, sound, etc...), so if you could give me an example of how to do both types that would be great.
cheers,
Andy
|
|
|
|
|
// given a 16-bit WORD (unsigned short)
WORD wHiLow;
// reverse the two bytes in the word
WORD wLowHi = MAKEWORD( HIBYTE(wHiLow), LOBYTE(wHiLow) );
// obtain binary string
char szBuffer[32];
// binary is radix of 2
itoa(wLowHi, szBuffer, 2);
|
|
|
|
|
Hi All,
This may be a silly question, but how do you seek in a large file. I have to deal with file sizes of 3GB to 5GB and my programme currently uses the C-runtime fopen, fread, fseek, ftell, etc. But with files that large the sizes are beyond the size of 'long' that these functions use. Are there ways around this? (The programme has to run on Win98 - WinXP) I need to be able to get the size of the file, and be able to seek to different parts of it.
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Try Windows API,
GetFileSize
Support 64 bits file size
|
|
|
|
|
Thanks, this looks good
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
I don't have any sample code for you.
But to manipulate large files, you should throw C functions away, and
use Windows API to do it, open, seek, get file size, read/write, etc.
It's not difficult to learn those APIs by yourself.
|
|
|
|
|
Koms Bomb wrote:
It's not difficult to learn those APIs by yourself.
Okay, just one question:
C-Runtime provide feof, what is the windows API version of this?
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
RichardS wrote:
C-Runtime provide feof, what is the windows API version of this?
When using ReadFile() , consider this from MSDN:
If the return value is nonzero and the number of bytes read is zero, the file pointer was beyond the current end of the file at the time of the read operation. However, if the file was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the return value is FALSE and GetLastError returns ERROR_HANDLE_EOF when the file pointer goes beyond the current end of file.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Hi David,
Thanks for the info
regards,
Rich
"Programming today is a race between software engineers striving to build bigger and
better idiot-proff programs, and the Universe trying to produce bigger and better idiots.
So far the Universe is winning." -- Rich Cook
|
|
|
|
|
Another way,
GetFileSize to get the file size,
Then use SetFilePointer to 'seek' the file pointer, but you should only seek with zero offset from currect point, then that API will tell you where the currect pointer is.
Now compare that pointer with the file size...
So you don't need to read to the end of the file.
|
|
|
|