Converting IDC_Edit text to hexadecimal WORD - c++

I have an IDC_Edit text box set which will accept hexadecimal space-separated values that I'm wanting to convert into a WORD. I'm wanting the control to mimic the array, but I'm really unsure how to complete the conversion.
Basically, if I input:
"12 AB"
I need the resulting WORD to equal:
0x12AB
As someone who rarely dabbles with C++, let alone the WinAPI, I'm really stumped as to how to do this. My current code is:
HWND hTextInput = GetDlgItem(hWnd, IDC_EDIT);
DWORD dwInputLength = Edit_GetTextLength(hTextInput);
char* resultString = new char[dwInputLength+1];
memset(hTextInput , 0, dwInputLength+1);
WORD result = (resultString[0] << 8) | resultString[1];
This pulls the IDC_EDIT control's text and length and turns it into a char* array. This then attempts to convert into a WORD, but obviously this only currently takes the first two characters (12) in this case.
How can I make this pull "12 AB" into the char* array as [0x12, 0xAB] (rather than ["1", "2", "A", "B"]) so that I can then shift the two bytes into a WORD?

Try this:
WORD Readhex(const char *p)
{
char c ;
WORD result = 0 ;
while (c = *p++)
{
if (c >= '0' && c <= '9')
c -= '0' ;
else if (c >= 'A' && c <= 'F')
c -= 'A' - 10 ;
else if (c >= 'a' && c <= 'f')
c -= 'a' - 10 ;
else
continue ;
result = (result << 4) + c ;
}
return result ;
}
...
HWND hTextInput = GetDlgItem(hWnd, IDC_EDIT);
DWORD dwInputLength = Edit_GetTextLength(hTextInput);
char* resultString = new char[dwInputLength+1];
GetWindowText(hTextInput, resultString, dwInputLength+1) ;
WORD result = ReadHex(resultString) ;
You also forgot the GetWindowText; and filling the buffer with zeros (memset) is not necessary.
The strtol function as suggested will not work for you because strtol will consider the space as a terminator. Maybe you should also consider to do some error checking, for example if the user enters garbage. The ReadHex function above simply ignores any non hex digits.

Related

Can Someone explain this Program ? I don't understand it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is the program for converting the ascii sentence to character sentence. But i dont understand the num = num * 10 + (str[i] - '0'); why it us used and why it's needed?
#include <bits/stdc++.h>
using namespace std;
void asciiToSentence(string str, int len)
{
int num = 0;
for (int i = 0; i < len; i++) {
num = num * 10 + (str[i] - '0');
if (num >= 32 && num <= 122) {
char ch = (char)num;
cout << ch;
num = 0;
}
}
}
int main()
{
string str = "973265";
int len = str.length();
asciiToSentence(str, len);
return 0;
}
Firstly, bits/* are not standard headers, you shouldn't be using them otherwise your code may not compile with compilers like MSVC. Refer this.
Now let's come to your query : str[i] - '0' is simply a way to convert char to respective int, i.e. '0' to 0. You can find more discussion on this thread.
What does your code do?
It just keeps on extracting chars from your string (in loop), converting it to int and adding to num after multiplying it by 10. This is basically converting a part of string, say, "97" to an integer 97.
When num comes in range from 32 (' ') to 122 ('Z') (both inclusive), it cast them to char and prints it.
Explanation of Sample Output:
Your code prints a A to console. You will get this result if you can figure out the substrings. Here consider these three - "97" "32" "65". Then "97" will be converted to 97 (as int), then will be cast to char and end up being 'a' ('a' has ASCII value of 97). Similarly "32", will be a space ' ', and "65" will be 'A'.
These substrings didn't come here by magic. Let me explain one for you :
You have str = "973265", num = 0, then the loop begins :
i = 0 -> str[i] = '9' -> str[i] - '0' = 9 -> num = 0*10 + 9 = 9 -> is num in range [32, 122]? false -> go to next iteration.
i = 1 -> str[i] = '7' -> str[i] - '7' = 7 -> num = 9*10 + 7 = 97 -> is num in range [32, 122]? true -> ch = (char)num = (char)97 = 'A' -> print ch, i.e. 'A' -> num = 0 -> go to next iteration.
Pro Tip: Use your debugger, set breakpoints, watch variables and step through your code for better understanding.
Note that: When I wrote ____ - I meant ____ like this ____.
"convert" -- conversion -- '0' <-> 0 or "12" <-> 12
"cast" -- casting -- '0' <-> 48 (assuming your compiler is using ASCII).
In http://www.asciitable.com/, you can see that 0 has ascii value of 48, 1 is 49, 2 is 50 and so on.
So str[i] - '0' will give you 0 if str[i] == '0', 1 if str[i] == '1' and so on. This is a simple trick to convert numeric char into its mathematical value.
Let's call str[i] - '0' as x, so the line you don't understand becomes num = num * 10 + x. What this does is, take the original num (say it was 32), add a 0 behind it (so it becomes 320), then add x (if x is 7, num becomes 327).
So overall, this code is basically a function that converts a number string into integer type.

Specify a range of ASCII lowercase chars in C++

I am writing a program that takes a char and compares it to see if it's in a range of certain chars. For instance, if the char I get is an n I go to state 3, if its a - m or o - z I go to state 4. I'm new to C++ so I'm still learning.
Can I say something like:
char c = file.next_char();
...
if (c in 'a'...'m', 'o'...'z')
{
state = 3;
} else {
state = 4;
}
There is no such syntax in C++. The options are:
Use a switch statement, when the list of values is generally not contiguous, or
Convert the list of explicit character values into contiguous ranges into equivalent boolean expressions. As you know, alphabetic characters consist of a contiguous range of octets in ASCII, so your pseudo-code is equivalent to:
if ( (c >= 'a' && c <= 'm')
||
(c >= 'o' && c <= 'z'))
If you are using ascii (English), you can rely on the fact that all the lower case letters are adjacent. Just check 'a' <= c && c <= 'z'
after ruling out 'n'.
You never said what happens if the state is not one of those, so I left it alone.
// 3 and 4 mean nothing. Give your states meaningful names
enum state_type {FirstState, SecondState, ThirdState, FourthState};
state_type state = FirstState;
char c = get_next_char();
if ('n' == c){
state = FourthState;
} else if ('a' < c && c < 'z'){
state = ThirdState;
} else {
// no change?
}
You could maybe use a for loop to compare it to see if it's a letter in the first or second range. The code would be something this:
char range1[/*amount in array here*/] = "abcdefghijklmnopqrstuvwxyz";
//Substitute range on line above(the characters in the string)
for(int i = 0; i <= /*amount in array here*/; i++) {
if(range1[i] == /*nameoflettervariablehere*/){
//Code here
}
}
I'm sorry but I don't know of a more efficient way.

What approach should I take towards converting ascii chars to other chars in c++

Well currently I am re creating my own version of enigma as a little project but if you understand how the enigma machine works it has rotors which connect a character to a completely different character for example A might be connected to F or U may be connected to C and this is done three times. Currently I am getting the char for the rotor by using this function:
char getRotorOne(char i) {
if(i == 'a') {
return 'g';
}if(i == 'b') {
return 'A';
}if(i == 'c') {
return 'o';
}
The main problem with this is it takes a long time to write and it seems inefficient and I think there must be a better way. The other problem with this is on the original enigma machine there were only the 26 letters of the alphabet on this there are the 94 tapeable characters of ascii (32-126) is there any other simpler way that this can be done? If you think this question is unclear or you don't understand please tell me instead of just flagging my post, then you can help me improve my question.
Use tables! Conveniently, C string literals are arrays of characters. So you can do this:
// abc
const char* lower_mapping = "gAo";
// ABC
const char* upper_mapping = "xyz";
char getRotorOne(char i) {
if (i >= 'a' && i <= 'z') return lower_mapping[i - 'a'];
if (i >= 'A' && i <= 'Z') return upper_mapping[i - 'A'];
assert(false && "Unknown character cannot be mapped!");
}
Since chars are really just small integers, and ASCII guarantees contiguous ranges for a-z and A-Z (and 0-9) you can subtract from a given character the first one in its range (so, 'a' or 'A') to get an index into that range. That index can then be used to look up the corresponding character via a table, which in this case is just a simple hardcoded string literal.
This is an improvement on Cameron's answer. You should use a simple char array for each rotor, but as you said you want to process ASCII characters in the range 32-126, you should build each mapping as an array of 95 characters:
char rotor1[95] ="aXc;-0..."; // the 95 non control ascii characters in arbitrary order
Then you write your rotor function that way:
char getRotorOne(char i) {
if ((i < 32) || (i > 126)) return i; // do not change non processed characters
return rotor1[i - 32]; // i - 32 is in range 0 - 94: rotor1[i - 32] is defined
}

Homemade Vigenere cipher; working with acsii character manipulation

As the post title suggests, I'm working to strengthen my grasp on C++ and character manipulation, this time through creating a Vigenere Cipher. For those unfamiliar with it, it's a fairly simple way to encrypt a text file.
The basic way it works is that there exists a string "key", and each character (in my case at least) is a lowercase alphabetical character. These are stored into an array and are used to "shift" the value of the file being encoded. A character of 'a' will shift the target by 0, while 'z' will shift it by 25. The "shift" is cyclical, meaning that if 'z' is shifted by 'b' (1) it should result in an 'a'.
My current method is found below:
//Assume cipher[] contains "[a][b][c][x ][y ][z ]" Cipher is a <string> object
//Assume ptr[] contains "[0][1][2][23][24][25]
#A whole bunch of includes
char c;
ifstream is;
ofstream os;
is.open(argv[3]) //"myinput.txt"
os.open(argv[4]) //"myoutput.txt"
int i = 0;
while( is.good() ) {
c = is.get();
if( is.good() ) { //did we just hit the EoF?
c = tolower( c - 0 ); //just make sure it's lowercase
c = c + ptr[ i % cipher.size() ] % 26;
if( c> 122 )
c = ( c % 123 ) + 97;
i++;
os.put( c );
}
}
My problem lies in my modulo operations, I believe. Maybe it's because I've spent so much time hashing this out, but I spent hours last night writing this, and then another hour lying in bed trying to wrap my mind around how to effectively create what I want:
grab char.
check char. //let char = 'z'
check the cipher. //let the cipher = 'y'
eval cipher shift //'y' shift value = 24
shift z 24 places (cyclically) //'z'==25, 25+24=49, 49%26=23. 23='x'
HERE IS THE ISSUE: How to do this with ACSII? ('a'=97, z='121')
Imagine that you want to "shuffle" the "ones" digits 0-9 between 20 and 29 by two steps, such that 20 becomes 22, and 29 becomes 21,. How would you do that?
Well, I would subtract 20 [our base number], and then shuffle the remaining digit, and then add 20 back in again.
newnum = num - 20;
newnum %= 10;
newnum += 20;
The same principle would apply for ascii - just that of course the base isn't 20.

Caesar cipher in C++

To start off, I'm four weeks into a C++ course and I don't even know loops yet, so please speak baby talk?
Okay, so I'm supposed to read a twelve character string (plus NULL makes thirteen) from a file, and then shift the letters backwards three, and then print my results to screen and file. I'm okay with everything except the shifting letters. I don't want to write miles of code to take each character individually, subtract three, and re-assemble the string, but I'm not sure how to work with the whole string at once. Can someone recommend a really simple method of doing this?
If you are dealing with simple letters (A to Z or a to z), then you can assume that the internals codes are linear.
Letters are coded as numbers, between 0 and 127. A is coded as 65, B as 66, C as 67, Z as 90.
In order to shift letters, you just have to change the internal letter code as if it were a number, so basically just substracting 3 from the character. Beware of edge cases though, because substracting 3 to 'A' will give you '>' (code 62) and not 'X' (code 88). You can deal with them using "if" statements or the modulo operator ("%").
Here is an ASCII characters table to help you
Once you've loaded your string in, you can use the modulous operator to rotate while keeping within the confines of A-Z space.
I'd keep track of whether the letter was a capital to start with:
bool isCaps = ( letter >= 'A' ) && ( letter <= 'Z' );
if( isCaps )
letter -= 'A'-'a';
and then just do the cipher shift like this:
int shift = -3;
letter -= 'a'; // to make it a number from 0-25
letter = ( letter + shift + 26 ) % 26;
// add 26 in case the shift is negative
letter += 'a'; // back to ascii code
finally finish off with
if( isCaps )
letter += 'A'-'a';
so, putting all this together we get:
char *mystring; // ciphertext
int shift = -3; // ciphershift
for( char *letter = mystring; letter; ++letter )
{
bool isCaps = ( *letter >= 'A' ) && ( *letter <= 'Z' );
if( isCaps )
*letter -= 'A'-'a';
letter -= 'a';
letter = ( letter + shift + 26 ) % 26;
letter += 'a';
if( isCaps )
letter += 'A'-'a';
}
You're going to have to learn loops. They will allow you to repeat some code over the characters of a string, which is exactly what you need here. You'll keep an integer variable that will be your index into the string, and inside the loop do your letter-shifting on the character at that index and increment the index variable by one until you reach NULL.
Edit: If you're not expected to know about loops yet in your course, maybe they want you to do this:
string[0] -= 3; // this is short for "string[0] = string[0] - 3;"
string[1] -= 3;
string[2] -= 3;
...
It will only result in 12 lines of code rather than miles. You don't have to "reassemble" the string this way, you can just edit each character in-place. Then I bet after making you do that, they'll show you the fast way of doing it using loops.
Iterate over the characters with a for loop. And do what you want with the char*. Then put the new char back.
for(int i=0; i<12; i++){
string[i] = string[i] - 3;
}
Where string is your character array (string). There is a bit more involved if you want to make it periodic (I.E. have A wrap round to Z, but the above code should help you get started)
I'm a little unclear what you mean by "shift the letters backwards 3"?
Does that mean D ==> A?
If so, here's a simple loop.
(I didn't do reading from the file, or writing to the file... Thats your part)
#include <string.h>
int main(void)
{
char input[13] = "ABCDEFGHIJKL";
int i;
int len = strlen(input);
for(i=0; i<len; ++i)
{
input[i] = input[i]-3;
}
printf("%s", input); // OUTPUT is: ">?#ABCDEFGHI"
}