UDF decimal to binary - c++

I wrote a decimal to binary converter function in order to practice my manipulation of number systems and arrays. I took the int a converted it to binary and stored each character, or so I beleive, in an array, then displayed to the screen, however it is displaying characters I do not know i looked them up on the aski table and do not recognize them, so i would like to ask for your assistance, here is a picture of the code, and console app.
Thanks in advance.

You likely want to insert number chars (such as '1') in your result, but you assign the char value. Try adding the value of '0' to get a readable result (remainder + '0').
If you interpret the result array as a string (that's what i suggested), you should also set the last char to the value 0 (not '0'!) to mark the end of the c string.

Your output function not correct output your binary text because:
1) cout output characters until '\0', so your function will correct output until get first 0 in binary representation of int (for example for 5 = 101 it will output only one smile with code 0x01).
2) your last character in array is not '\0', so cout will output garbage until '\0' or memory access exception.

Related

What's the difference between LEN and <>"" when searching for values?

I initially learned that if I want to see if a cell has any contents to use if(A1<>"",.... But as I receive more and more assistance on SO, it seems most people use if(LEN(A1),... Is there a difference? Did I learn the wrong information? Should I ever opt for one over the other or just always use LEN from now on?
pretty much the same result. difference is:
LEN(A1) - checks if A1 has a length
A1<>"" - checks if A1 is not equal to "empty"
then there is a length of the formula itself (some prefer to save 1 extra character space):
A1<>"" has 6 characters compared to LEN(A1) 7 characters
the superiority of LEN comes when you need to check for character count like:
=IF(LEN(A1)=4, TRUE, FALSE)
eg. output TRUE only if A1 value has exactly 4 characters

convert a string to a sequence of binary

i want to convert any char to its binary representation(not a string like my cuurent code does now) it needs to be a sequence of binary numbers
after that i will take every 16 bits from what i've done and calculate their sum
i cant use numpy or any other package
this is what i got now and it
def checksum(st):
data = ' '.join(map(bin,bytearray(st)))
binar = [data[i:i+16] for i in range(0, len(data), 16)]
check = 0xffff
for hex in binar:
check += int(hex,2)
return check
my current code gets a string (for example:'10100/01') and i want to sum every 16 bits of the string therefor i need to convert the string to binary numbers and then sum every 16 bits together
This answers your question, assuming I understood it properly. The first two lines of your code don't seem to do what you want to achieve, but maybe you just forgot mentioning something.
Anyhow.
def checksum2(st):
dummy = 0xFFFF
for count in xrange(0,len(st),2):
dummy += ord(st[count])+ord(st[count+1])*256
return dummy
This code steps through every second char of your string and adds the value of one char to the value of the next char times 256, which creates a word. Remove the *256 if you didn't actually want to create a proper 16bit value and instead only wanted to add two 8bit values together. And if you rather need a big endian, instead of little endian, then just move the *256 the the other ord().

C++ - A few quetions about my textbook's ASCII table

In my c++ textbook, there is an "ASCII Table of Printable Characters."
I noticed a few odd things that I would appreciate some clarification on:
Why do the values start with 32? I tested out a simple program and it has the following lines of code: char ch = 1; std::cout << ch << "\n"; of code and nothing printed out. So I am kind of curious as to why the values start at 32.
I noticed the last value, 127, was "Delete." What is this for, and what does it do?
I thought char can store 256 values, why is there only 127? (Please let me know if I have this wrong.)
Thanks in advance!
The printable characters start at 32. Below 32 there are non-printable characters (or control characters), such as BELL, TAB, NEWLINE etc.
DEL is a non-printable character that is equivalent to delete.
char can indeed store 256 values, but its signed-ness is implementation defined. If you need to store values from 0 to 255 then you need to explicitly specify unsigned char. Similarly from -128 to 127, have to specify signed char.
EDIT
The so called extended ASCII characters with codes >127 are not part of the ASCII standard. Their representation depends on the so called "code page" chosen by the operating system. For example, MS-DOS used to use such extended ASCII characters for drawing directory trees, window borders etc. If you changed the code page, you could have also used to display non-English characters etc.
It's a mapping between integers and characters plus other "control" "characters" like space, line feed and carriage return interpreted by display devices (possibly virtual). As such it is arbitrary, but they are organized by binary values.
32 is a power of 2 and an alphabet starts there.
Delete is the signal from your keyboard delete key.
At the time the code was designed only 7 bits were standard. Not all bytes (parts words) were 8 bits.

Data not saved in binary form

I made a program as below
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<fstream.h>
void main() {
char name[24];
cout << "enter string :";
gets(name);
ofstream fout;
fout.open("bin_data",ios::out|ios::binary);
fout.write((char*)&name,10);
fout.close();
}
But when I open the file bin_data by notepad I find that the string is saved in text format not in binary form...... Please help...
This code can save a word of 10 char.
But when I compile this code by turbo c++ v4.5 I find that. When I input 1 or 2 letter word it saves in text format(ignore garbage value) but when I input a word of 3 to 7 letter long it saves in binary format. and in 9 and 10 letter word again in text format..... Can anyone tell me the reason...?
Please compile and run program as I mentioned above and answer
Your data only contains text. It is represented by the very same bits in both text format and binary format.
Binary format means that your data is written to the file unchanged. If you were to use text format, some non-text characters would be modified. For example, byte 10 (which represents newline) could be changed to operating system specific newline (two bytes, 15 and 10, on Windows).
For binary values of text characters, see http://www.asciitable.com/
Your second example has a buffer overflow.
char name[24];
fout.write((char*)&name,10);
You reserve 24 bytes of data, which is filled by random bytes that happen to be at that point of memory. When you save a 2-character string to the buffer, it only overwrites first three bytes. The third byte is set to value 0, which tells you that the text ends at that point. If you were to call strlen(), it would tell you the amount of characters before the first 0 byte.
If your input is a 2-character text, and you choose to write 10 bytes from your buffer, the 7 bytes in the end are filled with invalid data. Note that this does not cause an access violation, because you have reserved data for 24 bytes.
See also: https://en.wikipedia.org/wiki/Null-terminated_string

Convert Extended characters to int

I have the following string in notepad "ùŒÚÿwž+2»ó66H".
I used the fstream library to read those files in c++ and print those characters and thier equivalent decimal numbers in the conlsole window but the symbols are different than those in notepad and numbers for the extended characters are in negative form,i realize maybe its impossible for my console window to print those symbols as the vary through many character sets but how do i get the numbers displayed as 255 and not -1 ??
Simple version: Read the file as unsigned char instead of char and use printf('%c', a) to see what character you get. This will get you values between 0 to 255 and not -128 to 127