Truncating bytes

Mar 23, 2012 at 7:35pm
I can't think of how to go about this so I decided it would be quicker to just ask the experts. I have a function that generates 64bit signed integer, what I want to do is get the correct hex for it. Example
1
2
3
4
5
6
7
8
9
10
11
12
u64 value;
s8 v8 = v64;
s16 v16 = v64;
s32 v32 = v64;
switch ( valueSize )
{
   case 1u: value = (u8)v8; break;
   case 2u: value = (u16)v16; break;
   case 4u: value = (u32)v32; break;
   default: value = (u64)v64;
}
return value;

I have a search GUI and the values are grabbed before the loop begins, the loop grabs hold of ramBuff#[ i ] and puts it in ramValue before begining tests against oldValue which is taken from oldBuff#[ i ], goodValue and badValue before finally setting ramAddress[ j ].
i and j are used because the ram that is retrieved is a section at a time so i only increments when get to the next address usable based on selected size.
I've tried looking for this information on the net but too much junk info comes out.
Mar 23, 2012 at 9:43pm
One way is to use << and >> for shift left bits and shift right bits. For example , if you shift a 64 bit int to the left by 56 bits and back the 56. you get an 8 bit number:

 
long int myU64 = (u64)3244323424234223<<56;


then shift the 56 back to the right.
 
myU64 = myU64>>56;


if we output as an unsigned it should be between 0-255.

Last edited on Mar 23, 2012 at 9:45pm
Mar 23, 2012 at 11:21pm
Thanks but it didn't work, I tried another way had more progress but still not quite the desired result but worked slightly better.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#define SINT8_MAX       127
#define SINT8_MIN      -127
#define SINT8_POS     0x80
#define SINT16_MAX      32767
#define SINT16_MIN     -32767
#define SINT16_POS    0x8000
#define SINT32_MAX      2147483647
#define SINT32_MIN     -2147483647
#define SINT32_POS    0x80000000
#define SINT64_MAX      3028092406290448383LL
#define SINT64_MIN     -3028092406290448383LL
#define SINT64_POS    0x8000000000000000LL
using namespace std;
typedef int8_t s8;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint32_t u32;
typedef uint64_t u64;
u64 getHEXFromDecimal( char* text );
s64 getHEXFromSignedDecimal( char* text );
int main()
{
	u8 i, j;
	u64 *hex = new u64[4];
	hex[0] = 1u;
	hex[1] = 2u;
	hex[2] = 4u;
	hex[3] = 8u;
	u64 shiftBy;
	u8 size = 2u;
	s8 v8hs = 0u, v8ls = 0u;
	for ( i = 0u; i < 4u; i++ )
	{
		for ( j = 0u; j < size; j++ )
		{
			shiftBy = hex[ i ];
			shiftBy << j;
			v8hs += ( 30LL & shiftBy );
			v8ls += ( -30LL & shiftBy );
			printf( "\ni: %u; j: %u; v8hs: %i;\tv8ls: %i", i, j, v8hs, v8ls );
		}
	}
	// Should produce -30 here
	v8ls = ( SINT64_POS & -30LL > 0u ) ? v8ls : -v8ls;
	u8 v8hu =   ( u8)v8hs;
	u8 v8lu =   ( u8)v8ls;
	printf( "\ns8: %i, %i", v8hs, v8ls );
	//cout << "s8: " << v8hs << ", " << v8ls;
	cout << "\nu8: " << v8hu << ", " << v8lu;
	delete [] hex;
	return 0;
}
Mar 24, 2012 at 8:42am
Found a way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main()
{
	u8 i = 0u;
	s8 v8s = 0x0;
	u8 v8u = 0u;
	s64 v64s = -130;
	s64 limitPos = SINT8_MAX;
	s64 limitNeg = SINT8_MIN;
	u64 shiftBy = 0u;
	if ( v64s > limitPos )
	{
		v8s = SINT8_MAX;
	}
	else if ( v64s < limitNeg )
	{
		v8s =  SINT8_MIN;
	}
	else
	{
		for ( ; i < 8u; i++ )
		{
			shiftBy = 1u;
			shiftBy <<= i;
			v8s = v8s | ( v64s & shiftBy );
			printf( "\nv8s: %02X;\tshiftBy: %02X", v8s, shiftBy );
		}
		//v8s = ( SINT64_POS & v64s > 0u ) ? -v8s : v8s;
	}
	v8u = 0u | v8s;
	printf( "\nv64s: %016llX", v64s );
	printf( "\nv8s: %02i;\tv8u: %02u", v8s, v8u );
	printf( "\nv8s: %02X;\tv8u: %02X", v8s, v8u );
    return 0;
}
Topic archived. No new replies allowed.