Determining byte values per character

May 3, 2010 at 9:53am
How do I determine the byte values of an array?
The function sizeof() determines the size right? not the actual byte values.
Is there a function in determining byte values?

let say we have this code..
1
2
3
4
5
6
7
8
9
10
11
12
int i, helLen, byteVal;
char detByte[100];
string hello = "Hello"

sprintf(detByte, "%s", hello);
helLen = strlen(hello);
for(i=0;i<helLen;i++){
   //insert a "function" that determine byte value per character
   byteVal[i] = function(detByte[i]);
} 

Thanks for the help.
May 3, 2010 at 10:59am
The function sizeof() determines the size right? not the actual byte values.
sizeof actually determines the number of bytes. If you're working with a char array it happens that it also returns the number of elements because in most compilers it's sizeof(char)==1.

Consider this example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
     long array[10];

     //this should say 4 (or more)
     cout << "sizeof(long)=" << sizeof(long) << endl;

     //this should say 40 (or more)
     cout << "sizeof(array)=" << sizeof(array) << endl;
     
     //this should say 10
     cout << "sizeof(array)/sizeof(long)=" << sizeof(array)/sizeof(long) << endl;

     cin.get();
}
Last edited on May 3, 2010 at 11:01am
May 3, 2010 at 12:08pm
The byte value is simply its integer value.
1
2
char c = 'A';
cout << c << " has the value " << (int)c << ".\n";
May 3, 2010 at 12:49pm
or cout << 'A'-0;
Topic archived. No new replies allowed.