搜尋此網誌
2010年6月23日 星期三
如何將整數值放入Char陣列中,並還原其值
主要是用在通訊系統,一般而言我們透過序列埠傳送資料時只能傳送BYTE array的資料,所以當我們要傳送一個整數的資料時,就必須將整數資料放在BYTE array中傳送.
但要如何將整數資料放在 BYTE array中呢?
一般方法有兩種
1. 轉成16進制字元陣列 (較節省空間,但運算較複雜)
2. 採用BCD編碼傳送 (較直覺,但浪費空間)
16進制字元陣列:
例如有一個值為 1454591794 其16進制為 0x56B34F32;
我們可以宣告一個char的陣列其大小為4 , 如 char hexStr[4]來儲存; (4個byte剛好為一個int)
其內容分別為 hexStr[0] = 0x32, hexStr[1] = 0x4F, hexStr[2] = 0xB3, hexStr[3] = 0x56
反之我們可以使用下面的方法將 16進制char陣列 還原成int的值
unsigned int tt = (unsigned int) hexStr[0];
unsigned int t2;
tt <<= 24;
t2 = (unsigned int) hexStr[1];
tt |= (t2 << 16);
t2 = (unsigned int) hexStr[2];
tt |= (t2 << 8);
t2 = (unsigned int) hexStr[3];
tt |= t2;
其中 tt就是我們要的整數值
BCD方式如下:
假設值為 1454591794
我們可以將其拆解為 0x14 , 0x54, 0x59, 0x17, 0x94
我們可以宣告一個char的陣列其大小為5 , 如 char hexStr[5]來儲存
其內容分別為
hexStr[0] = 0x94, hexStr[1] = 0x17.........hexStr[4] = 0x14
還原整數方法如下:
char Buf[11];
sprintf(Buf, "%02x%02x%02x%02x%02x",
(unsigned char) hexStr[4],
(unsigned char) hexStr[3],
(unsigned char) hexStr[2],
(unsigned char) hexStr[1],
(unsigned char) hexStr[0]);
int i = atoi(Buf);
其中 i就是我們要的整數值
2010年4月25日 星期日
肚皮舞者
2010年4月18日 星期日
走向共和
2010年4月7日 星期三
Returning values by value, reference, and address
Return by value
Return by value is the simplest and safest return type to use. When a value is returned by value, a copy of that value is returned to the caller. As with pass by value, you can return by value literals (eg. 5), variables (eg. x), or expressions (eg. x+1), which makes return by value very flexible.
Another advantage of return by value is that you can return variables (or expressions) that involve local variables declared within the function. Because the variables are evaluated before the function goes out of scope, and a copy of the value is returned to the caller, there are no problems when the variable goes out of scope at the end of the function.
1.
int
DoubleValue(
int
nX)
2.
{
3.
int
nValue = nX * 2;
4.
return
nValue;
// A copy of nValue will be returned here
5.
}
// nValue goes out of scope here
Return by value is the most appropriate when returning variables that were declared inside the function, or for returning function arguments that were passed by value. However, like pass by value, return by value is slow for structs and large classes.
Return by reference
Just like with pass by reference, values returned by reference must be variables (you can not return a reference to a literal or an expression). When a variable is returned by reference, a reference to the variable is passed back to the caller. The caller can then use this reference to continue modifying the variable, which can be useful at times. Return by reference is also fast, which can be useful when returning structs and classes.
However, returning by reference has one additional downside that pass by reference doesn’t — you can not return local variables to the function by reference. Consider the following example:
1.
int
& DoubleValue(
int
nX)
2.
{
3.
int
nValue = nX * 2;
4.
return
nValue;
// return a reference to nValue here
5.
}
// nValue goes out of scope here
See the problem here? The function is trying to return a reference to a value that is going to go out of scope when the function returns. This would mean the caller receives a reference to garbage. Fortunately, your compiler will give you an error if you try to do this.
Return by reference is typically used to return arguments passed by reference to the function back to the caller. In the following example, we return (by reference) an element of an array that was passed to our function by reference:
01.
// This struct holds an array of 25 integers
02.
struct
FixedArray25
03.
{
04.
int
anValue[25];
05.
};
06.
07.
// Returns a reference to the nIndex element of rArray
08.
int
& Value(FixedArray25 &rArray,
int
nIndex)
09.
{
10.
return
rArray.anValue[nIndex];
11.
}
12.
13.
int
main()
14.
{
15.
FixedArray25 sMyArray;
16.
17.
// Set the 10th element of sMyArray to the value 5
18.
Value(sMyArray, 10) = 5;
19.
20.
cout << sMyArray.anValue[10] << endl;
21.
return
0;
22.
}
This prints:
5
When we call Value(sMyArray, 10)
, Value() returns a reference to the 10th element of the array inside sMyArray. main() then uses this reference to assign that element the value 5.
Although this is somewhat of a contrived example (because you could access sMyArray.anValue directly), once you learn about classes you will find a lot more uses for returning values by reference.
Return by address
Returning by address involves returning the address of a variable to the caller. Just like pass by address, return by address can only return the address of a variable, not a literal or an expression. Like return by reference, return by address is fast. However, as with return by reference, return by address can not return local variables:
1.
int
* DoubleValue(
int
nX)
2.
{
3.
int
nValue = nX * 2;
4.
return
&nValue;
// return nValue by address here
5.
}
// nValue goes out of scope here
As you can see here, nValue goes out of scope just after its address is returned to the caller. The end result is that the caller ends up with the address of non-allocated memory, which will cause lots of problems if used. This is one of the most common programming mistakes that new programmers make. Many newer compilers will give a warning (not an error) if the programmer tries to return a local variable by address — however, there are quite a few ways to trick the compiler into letting you do something illegal without generating a warning, so the burden is on the programmer to ensure the address they are returning will be to a valid variable after the function returns.
Return by address is often used to return newly allocated memory to the caller:
01.
int
* AllocateArray(
int
nSize)
02.
{
03.
return
new
int
[nSize];
04.
}
05.
06.
int
main()
07.
{
08.
int
*pnArray = AllocateArray(25);
09.
// do stuff with pnArray
10.
11.
delete
[] pnArray;
12.
return
0;
13.
}
Conclusion
Most of the time, return by value will be sufficient for your needs. It’s also the most flexible and safest way to return information to the caller. However, return by reference or address can also be useful, particularly when working with dynamically allocated classes or structs. When using return by reference or address, make sure you are not returning a reference to, or the address of, a variable that will go out of scope when the function returns!
摘自: http://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address
2010年3月5日 星期五
What is heap and stack?
What is heap and stack?
The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored. The heap is the section of computer memory where all the variables created or initialized at runtime are stored.
What are the memory segments?
The distinction between stack and heap relates to programming. When you look at your computer memory, it is organized into three segments:
- text (code) segment
- stack segment
- heap segment
The text segment (often called code segment) is where the compiled code of the program itself resides. When you open some EXE file in Notepad, you can see that it includes a lot of "Gibberish" language, something that is not readable to human. It is the machine code, the computer representation of the program instructions. This includes all user defined as well as system functions.
Now let's get to some details.
What is stack?
The two sections other from the code segment in the memory are used for data. The stack is the section of memory that is allocated for automatic variables within functions.
Data is stored in stack using the Last In First Out (LIFO) method. This means that storage in the memory is allocated and deallocated at only one end of the memory called the top of the stack. Stack is a section of memory and its associated registers that is used for temporary storage of information in which the most recently stored item is the first to be retrieved.
What is heap?
On the other hand, heap is an area of memory used for dynamic memory allocation. Blocks of memory are allocated and freed in this case in an arbitrary order. The pattern of allocation and size of blocks is not known until run time. Heap is usually being used by a program for many different purposes.
The stack is much faster than the heap but also smaller and more expensive.
Heap and stack from programming perspective
Most object-oriented languages have some defined structure, and some come with so-called main() function. When a program begins running, the system calls the function main() which marks the entry point of the program. For example every C, C++, or C# program must have one function named main(). No other function in the program can be called main(). Before we start explaining, let's take a look at the following example:
int x; /* static stack storage */
void main() {
int y; /* dynamic stack storage */
char str; /* dynamic stack storage */
str = malloc(50); /* allocates 50 bytes of dynamic heap storage */
size = calcSize(10); /* dynamic heap storage */
When a program begins executing in the main() function, all variables declared within main() will be stored on the stack.
If the main() function calls another function in the program, for example calcSize(), additional storage will be allocated for the variables in calcSize(). This storage will be allocated in the heap memory segment.
Notice that the parameters passed by main() to calcSize() are also stored on the stack. If the calcSize() function calls to any additional functions, more space would be allocated at the heap again.
When the calcSize() function returns the value, the space for its local variables at heap is then deallocated and heap clears to be available for other functions.
The memory allocated in the heap area is used and reused during program execution.
It should be noted that memory allocated in heap will contain garbage values left over from previous usage.
Memory space for objects is always allocated in heap. Objects are placed on the heap.
Built-in datatypes like int, double, float and parameters to methods are allocated on the stack.
Even though objects are held on heap, references to them are also variables and they are placed on stack.
The stack segment provides more stable storage of data for a program. The memory allocated in the stack remains in existence for the duration of a program. This is good for global and static variables. Therefore, global variables and static variables are allocated on the stack.
Why is stack and heap important?
When a program is loaded into memory, it takes some memory management to organize the process. If memory management was not present in your computer memory, programs would clash with each other leaving the computer non-functional.
Heap and stack in Java
When you create an object using the new operator, for example myobj = new Object();, it allocates memory for the myobj object on the heap. The stack memory space is used when you declare automatic variables.
Note, when you do a string initialization, for example String myString;, it is a reference to an object so it will be created using new and hence it will be placed on the heap.
摘自: http://www.maxi-pedia.com/what+is+heap+and+stack2010年1月17日 星期日
真是至理名言阿...
機會,就像老二一樣,緊握就會變大!
生活就像是被強姦,反抗不了就學著享受!
學習就像嫖妓,出錢又出力!
工作就像輪姦,如果你不行,就換另一個人來做!
社會就像手淫,全部的事情都要靠自己的雙手去解決!
發薪水就像是月經,一個月不來那麼一次總覺得不能安心!
兄弟就像保險套,插多大的洞都幫你罩著!
就算要fuck,起初也要有fu!
就算是lover,最後還是有個over!
就算是Believe,中間還是有個lie!
承諾,就像一句幹你娘,人人都會說,卻沒人做得到!